3172: [Tjoi2013] Word time limit:10 Sec Memory limit:512 MB
submit:2567 solved:1200
[Submit] [Status] [Discuss] Description
Someone reads a paper, a paper is made up of many words. But he found that a word would appear many times in the paper, and now wondered how many times each word appeared in the paper.
Input
The first integer n, which represents how many words, and the next n rows of one word per line. Each word is made up of lowercase letters, n<=200, and the word length does not exceed 10^6
Output
outputs n integers, and the number in line I indicates how many times the first word appears in the article.
Sample Input3
A
Aa
AAASample Output6
3
1Hintsourcesolution
Acmachine template problems, and do not match.
Build the trie tree, construct the fail pointer, and in the process of building the fail pointer, accumulate the number of times you can
Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespacestd;#defineN 1000010intsz,son[n][ -],fail[n],q[n],sum[n],loc[n];intNCharS[n];voidClear () {sz=1; for(intI=0; i< -; i++) son[0][i]=1;}voidInsertint&POS) { intx=1; for(intI=0; I<strlen (s); i++) { if(!son[x][s[i]-'a']) son[x][s[i]-'a']=++sz; X=son[x][s[i]-'a']; sum[x]++; } POS=x;}voidBuildfail () {intHead=0, tail=1; q[0]=1; fail[1]=0; while(head<tail) { intnow=q[head++]; for(intI=0; i< -; i++) { if(!son[now][i])Continue; intff=Fail[now]; while(!son[ff][i]) ff=FAIL[FF]; Fail[son[now][i]]=Son[ff][i]; Q[tail++]=Son[now][i]; } } for(inti=tail-1; i>=0; i--) Sum[fail[q[i] ]+=Sum[q[i]];}intMain () {clear (); scanf ("%d",&N); for(intI=1; i<=n; i++) scanf ("%s", s), insert (Loc[i]); Buildfail (); for(intI=1; i<=n; i++) printf ("%d\n", Sum[loc[i]]); return 0;}
A long time ago, when using automata, there were strange errors ... interesting ... Mdzz
"BZOJ-3172" Word AC automatic machine