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
| #include <bits/stdc++.h> using namespace std;
const int N = 1e6 + 5; int n, tr[N][26], fail[N], id[N], tot, stk[N], top, o[N]; char s[N], qaq[N];
void insert(const char *s) { int u = 0, n = strlen(s + 1); for (int i = 1; i <= n; i++) { int c = s[i] - 'a'; if (!tr[u][c]) tr[u][c] = ++tot; u = tr[u][c]; } id[u] = n; } void build() { queue<int> q; for (int i = 0; i < 26; i++) if (tr[0][i]) q.push(tr[0][i]); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; i++) { if (tr[u][i]) fail[tr[u][i]] = tr[fail[u]][i], q.push(tr[u][i]); else tr[u][i] = tr[fail[u]][i]; } } }
int main() { scanf("%s", qaq + 1); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%s", s + 1), insert(s); build(); int m = strlen(qaq + 1), u = 0; for (int i = 1; i <= m; i++) { int c = qaq[i] - 'a'; u = tr[u][c]; stk[++top] = u; o[top] = i; if (id[u]) { top -= id[u]; u = stk[top]; } } for (int i = 1; i <= top; i++) printf("%c", qaq[o[i]]); return 0; }
|