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
| #include <bits/stdc++.h>
using namespace std; int n, m;
void Next(string &p, vector<int> &ne){ for (int i = 1, j = 0; i < n; i ++){ while (j && p[i] != p[j]) j = ne[j - 1]; if (p[i] == p[j]) j ++; ne[i] = j; } }
void KMP (string &s, string &p, int begin, string q){ vector <int> ne(n); Next (p, ne); for (int i = begin, j = 0; i < m; i ++){ while (j && s[i] != p[j]) j = ne[j - 1]; if (s[i] == p[j]) j ++; if (j == n){ for (int k = 0; k < i - n + 1; k++) cout << s[k]; cout << q; for (int k = i + 1; k < m; k++) cout << s[k]; cout << endl; return ; } } cout << s << endl; }
void solve () { string s, p, q; cin >> s >> p >> q; n = p.size (), m = s.size (); cout << s << endl; KMP (s, p, 0, q);
}
int main () { int t; cin >> t; while (t --) { solve (); } }
|