标签:limit code similar color and 实现 googl tle show
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 63773 Accepted Submission(s): 21193
Input
Output
Sample Input
Sample Output
1 #include<cstdio> 2 #include<cstring> 3 char s[60],m[1000010]; 4 int T,n,sz,ans; 5 int a[500010][27],q[500010],p[500010],d[500010]; 6 bool v[500010]; 7 void ins(){ 8 int now=1,l=strlen(s); 9 for(int i=0;i<l;i++){ 10 int t=s[i]-‘a‘+1; 11 if(a[now][t]) now=a[now][t]; 12 else now=a[now][t]=++sz; 13 } 14 d[now]++; 15 } 16 void acmach(){ 17 int head=0,tail=1,now; 18 q[0]=1,p[1]=0; 19 while(head<tail){ 20 now=q[head++]; 21 for(int i=1;i<=26;i++){ 22 if(!a[now][i]) continue; 23 int k=p[now]; 24 while(!a[k][i]) k=p[k]; 25 p[a[now][i]]=a[k][i]; 26 q[tail++]=a[now][i]; 27 } 28 } 29 } 30 void solve(){ 31 int k=1,l=strlen(m); 32 for(int i=0;i<l;i++){ 33 v[k]=1; 34 int t=m[i]-‘a‘+1; 35 while(!a[k][t]) k=p[k]; 36 k=a[k][t]; 37 if(!v[k]) for(int j=k;j;j=p[j]){ 38 ans+=d[j]; 39 d[j]=0; 40 } 41 } 42 printf("%d\n",ans); 43 } 44 int main(){ 45 scanf("%d",&T); 46 while(T--){ 47 sz=1,ans=0; 48 scanf("%d",&n); 49 for(int i=1;i<=26;i++) a[0][i]=1; 50 while(n--){ 51 scanf("%s",s); 52 ins(); 53 } 54 acmach(); 55 scanf("%s",m); 56 solve(); 57 for(int i=1;i<=sz;i++){ 58 p[i]=d[i]=v[i]=0; 59 for(int j=1;j<=26;j++) a[i][j]=0; 60 } 61 } 62 return 0; 63 }
hzwer学长助我1A.
标签:limit code similar color and 实现 googl tle show
原文地址:http://www.cnblogs.com/J-william/p/7163104.html