1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1e9+7; const int N = 1e5 + 10;
int main() { ios::sync_with_stdio(0); int _; cin >> _; while (_--) { int n, m; cin >> n >> m; map<string, tuple<int, int, int>> mp; for (int i = 1; i <= n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { if (j + 2 <= m) mp.insert({s.substr(j, 2), {j + 1, j + 2, i}}); if (j + 3 <= m) mp.insert({s.substr(j,3), {j+1, j+3, i}}); } } vector<int> dp(m+1); dp[0] = 1; string s; cin >> s; s = '#' + s; for (int i = 1; i <= m; i++) { if (i >= 2) { string tmp = s.substr(i - 1, 2); if (mp.count(tmp)) dp[i] |= dp[i-2]; } if (i >= 3) { string tmp = s.substr(i-2, 3); if (mp.count(tmp)) dp[i] |= dp[i-3]; } } if (!dp[m]) cout << -1 << endl; else { int now = m; vector<tuple<int,int,int>> ve; while (now) { if (dp[now-2]) { string tmp = s.substr(now-1, 2); ve.push_back(mp[tmp]); now -= 2; } else if (dp[now-3]) { string tmp = s.substr(now-2, 3); ve.push_back(mp[tmp]); now -= 3; } } reverse(ve.begin(), ve.end()); cout << ve.size() << endl; for (auto [l,r,i] : ve) { cout << l << ' ' << r << ' ' << i << endl; } } }
}
|