标签:spl src text 9.1 ios namespace max isp pre
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 36709 Accepted Submission(s): 8149
1 /** 2 完成时间:2018.9.12 3 依言 4 算法:AC自动机 5 */ 6 #include<iostream> 7 #include<queue> 8 #include<cstdio> 9 #include<cstring> 10 #include<algorithm> 11 using namespace std; 12 const int SON=128; 13 const int MAXN=200*500+50; 14 15 struct AC_Auto 16 { 17 int root,L; 18 int next[MAXN][SON]; 19 int fail[MAXN]; 20 int num[4]; 21 int number[MAXN]; 22 int New_Node() 23 { 24 for(int i=0;i < SON;++i) 25 next[L][i]=-1; 26 num[L]=0; 27 return L++; 28 } 29 void Initial() 30 { 31 L=0; 32 root=New_Node(); 33 } 34 void Build_Trie(const char *s,const int id) 35 { 36 int now=root; 37 while(*s != ‘\0‘) 38 { 39 int index=*s; 40 if(next[now][index] == -1) 41 next[now][index]=New_Node(); 42 now=next[now][index]; 43 s++; 44 } 45 num[now]=id;//将结束字符赋值为病毒编号 46 } 47 void Build_Fail() 48 { 49 queue<int >myqueue; 50 for(int i=0;i < SON;++i) 51 if(next[root][i] == -1) 52 next[root][i]=root; 53 else 54 { 55 fail[next[root][i]]=root; 56 myqueue.push(next[root][i]); 57 } 58 while(!myqueue.empty()) 59 { 60 int now=myqueue.front(); 61 myqueue.pop(); 62 for(int i=0;i < SON;++i) 63 if(next[now][i] == -1) 64 next[now][i]=next[fail[now]][i]; 65 else 66 { 67 fail[next[now][i]]=next[fail[now]][i]; 68 myqueue.push(next[now][i]); 69 } 70 } 71 } 72 int Query(const char *s) 73 { 74 int cnt=0; 75 int now=root; 76 bool vis[MAXN]; 77 memset(vis,false,sizeof(vis)); 78 while(*s != ‘\0‘) 79 { 80 int index=*s; 81 now=next[now][index]; 82 int temp=now; 83 while(temp != root && num[temp] != 0 && !vis[num[temp]]) 84 { 85 number[cnt++]=num[temp]; 86 temp=fail[temp]; 87 vis[num[temp]]=true; 88 } 89 s++; 90 } 91 return cnt; 92 } 93 }; 94 AC_Auto ac; 95 char buf[10010]; 96 97 int main() 98 { 99 int N; 100 scanf("%d",&N); 101 ac.Initial(); 102 for(int i=1;i <= N;++i) 103 { 104 scanf("%s",buf); 105 ac.Build_Trie(buf,i); 106 } 107 ac.Build_Fail(); 108 int M; 109 scanf("%d",&M); 110 int total=0; 111 for(int i=1;i <= M;++i) 112 { 113 scanf("%s",buf); 114 int cnt=ac.Query(buf); 115 if(cnt > 0) 116 { 117 total++; 118 sort(ac.number,ac.number+cnt); 119 printf("web %d:",i); 120 for(int i=0;i < cnt;++i) 121 printf(" %d",ac.number[i]); 122 printf("\n"); 123 } 124 } 125 printf("total: %d\n",total); 126 127 return 0; 128 }
标签:spl src text 9.1 ios namespace max isp pre
原文地址:https://www.cnblogs.com/violet-acmer/p/9637108.html