voidbuild(){ for (int i = 0; i < 26; i++) { if (tr[0][i]) q.push(tr[0][i]); } while (q.size()) { 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]; } } }
usingnamespacestd; usingnamespace FAST_IO; const ll mod = 1e9 + 7; constint INF = 0x3f3f3f3f; const ll INF_LL = 0x3f3f3f3f3f3f3f3f; constdouble eps = 1e-5; constint maxn = 1e6 + 10; constint maxm = 1e5 + 10; int t, n, m, k; structACTrie { conststaticint M = 26; conststaticint maxn = 1e6 + 10; int next[maxn][M] = {}, fail[maxn] = {}, cnt = 0; int exist[maxn] = {}; queue<int> q; voidinsert(char *s) { int now = 0, len = strlen(s); for (int i = 0; i < len; ++i) { int c = s[i] - 'a'; if (!next[now][c]) next[now][c] = ++cnt; now = next[now][c]; } exist[now]++; }
voidbuild() {
for (int i = 0; i < M; ++i) { if (next[0][i]) { fail[next[0][i]] = 0; q.push(next[0][i]); } } while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < M; ++i) { if (next[u][i]) { fail[next[u][i]] = next[fail[u]][i]; q.push(next[u][i]); } else next[u][i] = next[fail[u]][i]; } } }
intquery(char *s) { int now = 0, len = strlen(s), ans = 0; for (int i = 0; i < len; ++i) { int c = s[i] - 'a'; now = next[now][c]; for (int j = now; j && exist[j] != 0; j = fail[j]) { ans += exist[j]; exist[j] = 0; } } return ans; } }; char a[maxn]; intmain() { // #define COMP_DATA #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); cin >> n; ACTrie trie; for (int i = 0; i < n; ++i) { cin >> a; trie.insert(a); } trie.build(); cin >> a; cout << trie.query(a) << endl; return0; }
usingnamespacestd; usingnamespace FAST_IO; const ll mod = 1e9 + 7; constint INF = 0x3f3f3f3f; const ll INF_LL = 0x3f3f3f3f3f3f3f3f; constdouble eps = 1e-5; constint maxn = 1e6 + 10; constint maxm = 1e5 + 10; int t, n, m, k; structNode { conststaticint M = 26; int next[M]; int exist; int fail; } trie[maxn]; int cnt = 0; voidinsert(char *s) { int now = 0, len = strlen(s); for (int i = 0; i < len; ++i) { int c = s[i] - 'a'; if (!trie[now].next[c]) trie[now].next[c] = ++cnt; now = trie[now].next[c]; } trie[now].exist++; } queue<int> q; voidbuild() { for (int i = 0; i < 26; ++i) { int child = trie[0].next[i]; if (child) { trie[child].fail = 0; q.push(child); } } while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; ++i) { int child = trie[u].next[i]; if (child) { trie[child].fail = trie[trie[u].fail].next[i]; q.push(child); } else trie[u].next[i] = trie[trie[u].fail].next[i]; } } }
intquery(char *s) { int u = 0, len = strlen(s), ans = 0; for (int i = 0; i < len; ++i) { u = trie[u].next[s[i] - 'a']; for (int j = u; j && trie[j].exist; j = trie[j].fail) { ans += trie[j].exist; trie[j].exist = 0; } } return ans; } char a[maxn]; intmain() { // #define COMP_DATA #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> a; insert(a); } build(); cin >> a; cout << query(a) << endl; return0; }