intac_query() { len = s.size(); int now = 0, ans = 0, p; for(int i = 0; i < len; ++i) { id = s[i] - 'a'; while(ac[now].vis[id] == 0 && now != 0) now = ac[now].fail; now = ac[now].vis[id];
now = (now == 0) ? 0 : now; p = now; while(p != 0 && ac[p].en != -1) { ans += ac[p].en; ac[p].en = -1; p = ac[p].fail; } } return ans; }
模板2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
intac_query() { int len = s.size(); int now = 0, ans = 0; for(int i = 0; i < len; ++i) { now = ac[now].vis[s[i] - 'a']; for(int j = now; j && ac[j].en != -1; j = ac[j].fail) { ans += ac[j].en; ac[j].en = -1; } } return ans; }
int n; char s[2000050]; char t[2000050]; structnode { int vis[26]; int flag, ans, fail; }ac[200050]; queue<int> q; int in[200050]; int ans[200050]; int mp[200050]; int tot, id, len, u, v;
voidinsert_(char *str, int k) { u = 0; len = strlen(str); for(int i = 0; i < len; ++i) { id = str[i] - 'a'; if(!ac[u].vis[id]) ac[u].vis[id] = ++tot; u = ac[u].vis[id]; } if(!ac[u].flag) ac[u].flag = k; mp[k] = ac[u].flag; }
voidget_fail() { for(int i = 0; i < 26; ++i) { if(ac[0].vis[i]) { ac[ac[0].vis[i]].fail = 0; q.push(ac[0].vis[i]); } }
while(!q.empty()) { int u = q.front(); q.pop(); for(int i = 0; i < 26; ++i) { if(ac[u].vis[i]) { ac[ac[u].vis[i]].fail = ac[ac[u].fail].vis[i]; ++in[ac[ac[u].vis[i]].fail]; q.push(ac[u].vis[i]); } else ac[u].vis[i] = ac[ac[u].fail].vis[i]; } } }
voidac_query(char *str) { len = strlen(str); u = 0;
for(int i = 0; i < len; ++i) { id = str[i] - 'a'; u = ac[u].vis[id]; ac[u].ans++; } }
voidtopo() { for(int i = 1; i <= tot; ++i) if(!in[i]) q.push(i);
while(!q.empty()) { u = q.front(); q.pop(); ans[ac[u].flag] = ac[u].ans; v = ac[u].fail; in[v]--; ac[v].ans += ac[u].ans; if(!in[v]) q.push(v); } }
intmain() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%s", t); insert_(t, i); } get_fail(); scanf("%s", s); ac_query(s); topo(); for(int i = 1; i <= n; ++i) printf("%d\n", ans[mp[i]]); return0; }
int n; char s[2000050]; char t[2000050]; structnode { int vis[26]; int flag, ans, fail; }ac[200050]; queue<int> q; int in[200050]; int ans[200050]; int mp[200050]; int tot, id, len, u, v;
voidinsert_(char *str, int k) { u = 0; len = strlen(str); for(int i = 0; i < len; ++i) { id = str[i] - 'a'; if(!ac[u].vis[id]) ac[u].vis[id] = ++tot; u = ac[u].vis[id]; } if(!ac[u].flag) ac[u].flag = k; mp[k] = ac[u].flag; }
voidget_fail() { for(int i = 0; i < 26; ++i) { if(ac[0].vis[i]) { ac[ac[0].vis[i]].fail = 0; q.push(ac[0].vis[i]); } }
while(!q.empty()) { int u = q.front(); q.pop(); for(int i = 0; i < 26; ++i) { if(ac[u].vis[i]) { ac[ac[u].vis[i]].fail = ac[ac[u].fail].vis[i]; ++in[ac[ac[u].vis[i]].fail]; q.push(ac[u].vis[i]); } else ac[u].vis[i] = ac[ac[u].fail].vis[i]; } } }
voidac_query(char *str) { len = strlen(str); u = 0;
for(int i = 0; i < len; ++i) { id = str[i] - 'a'; u = ac[u].vis[id]; ac[u].ans++; } }
voidtopo() { for(int i = 1; i <= tot; ++i) if(!in[i]) q.push(i);
while(!q.empty()) { u = q.front(); q.pop(); ans[ac[u].flag] = ac[u].ans; v = ac[u].fail; in[v]--; ac[v].ans += ac[u].ans; if(!in[v]) q.push(v); } }
intmain() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%s", t); insert_(t, i); } get_fail(); scanf("%s", s); ac_query(s); topo(); for(int i = 1; i <= n; ++i) printf("%d\n", ans[mp[i]]); return0; }