for (int i=0;i<26;i++) if (tr[0][i]) q.push(tr[0][i]); int now; while (!q.empty()){ now = q.front(); q.pop(); int fai = fail[now]; for (int i=0;i<26;i++){ if (!tr[now][i]) tr[now][i] = tr[fai][i]; else{ fail[tr[now][i]] = tr[fai][i]; q.push(tr[now][i]); } } }
#include<bits/stdc++.h> usingnamespace std; constint N = 1e6+10; int n,tr[N][26],tot,fail[N],ans,exi[N]; char s[N]; queue<int> q; voidinsert(char *s){ int len = strlen(s+1),now = 0; for (int i=1;i<=len;i++){ int c = s[i]-'a'; if (!tr[now][c]) tr[now][c] = ++tot; now = tr[now][c]; } exi[now]++; } voidbuild(){ for (int i=0;i<26;i++) if (tr[0][i]) q.push(tr[0][i]); int now; while (!q.empty()){ now = q.front(); q.pop(); int fai = fail[now]; for (int i=0;i<26;i++){ if (!tr[now][i]) tr[now][i] = tr[fai][i]; else{ fail[tr[now][i]] = tr[fai][i]; q.push(tr[now][i]); } } } } voidquery(char *s){ int len = strlen(s+1); int now = 0; for (int i=1;i<=len;i++){ int c = s[i]-'a'; now = tr[now][c]; int tmp = now; while (tmp && exi[tmp]!=-1){ ans+=exi[tmp]; exi[tmp] = -1; tmp = fail[tmp]; } } } intmain(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n; for (int i=1;i<=n;i++){ cin>>(s+1); insert(s); } build(); cin>>(s+1); query(s); cout<<ans<<endl; return0; }
#include<bits/stdc++.h> usingnamespace std; constint N = 2e5+10,M = 2e6+10; int n,tr[N][26],fail[N],tot,mp[N],indeg[N],ans[N]; char s[M]; queue<int> q; voidinsert(char *s,int id){ int len = strlen(s+1),now = 0; for (int i=1;i<=len;i++){ int c = s[i]-'a'; if (!tr[now][c]) tr[now][c] = ++tot; now = tr[now][c]; } mp[id] = now; } voidbuild(){ for (int i=0;i<26;i++) if (tr[0][i]) q.push(tr[0][i]); int now; while (!q.empty()){ now = q.front(),q.pop(); int fa = fail[now]; for (int i=0;i<26;i++){ if (!tr[now][i]) tr[now][i] = tr[fa][i]; else{ fail[tr[now][i]] = tr[fa][i]; indeg[tr[fa][i]]++; q.push(tr[now][i]); } } } } voidquery(char *s){ int len = strlen(s+1),now = 0; for (int i=1;i<=len;i++){ int c = s[i]-'a'; now = tr[now][c]; ans[now]++; } } voidtopo(){ for (int i=1;i<=tot;i++){ if (!indeg[i]) q.push(i); } int now; while (!q.empty()){ now = q.front(),q.pop(); ans[fail[now]]+=ans[now]; indeg[fail[now]]--; if (!indeg[fail[now]]) q.push(fail[now]); } } intmain(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n; for (int i=1;i<=n;i++){ cin>>(s+1); insert(s,i); } build(); cin>>(s+1); query(s); topo(); for (int i=1;i<=n;i++) cout<<ans[mp[i]]<<"\n"; return0; }
#include<bits/stdc++.h> usingnamespace std; constint N = 1e4+10,M = 1e2+10,mod = 1e4+7; int n,m,tr[N][26],ok[N],fail[N],tot,f[M][N]; char s[N]; queue<int> q; voidinsert(char *s){ int len = strlen(s+1),now = 0; for (int i=1;i<=len;i++){ int c = s[i]-'A'; if (!tr[now][c]) tr[now][c] = ++tot; now = tr[now][c]; } ok[now] = 1; } voidbuild(){ for (int i=0;i<26;i++) if (tr[0][i]) q.push(tr[0][i]); int now; while (!q.empty()){ now = q.front(),q.pop(); int fa = fail[now]; for (int i=0;i<26;i++){ if (!tr[now][i]) tr[now][i] = tr[fa][i]; else{ fail[tr[now][i]] = tr[fa][i]; ok[tr[now][i]]|=ok[tr[fa][i]]; q.push(tr[now][i]); } } } } intksm(int x,int y){ int ret = 1,bas = x; while (y){ if (y&1) ret = ret*bas%mod; bas = bas*bas%mod; y>>=1; } return ret; } intmain(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n>>m; for (int i=1;i<=n;i++){ cin>>(s+1); insert(s); } build(); int ans = ksm(26,m); f[1][0] = 1; for (int i=1;i<=m;i++){ for (int j=0;j<=tot;j++){ for (int p=0;p<26;p++){ if (!ok[tr[j][p]]) f[i+1][tr[j][p]] = (f[i+1][tr[j][p]]+f[i][j])%mod; } } } for (int i=0;i<=tot;i++) ans = (ans-f[m+1][i])%mod; ans = (ans+mod)%mod; cout<<ans<<endl; return0; }