标签:des style blog java color strong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10478 Accepted Submission(s): 2724
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 #include <vector> 7 using namespace std; 8 9 char a[205]; 10 char str[10005]; 11 int visited[505]; 12 vector<int>ve[1005]; 13 14 struct node{ 15 int flag; 16 int id; 17 struct node *next[130], *fail; 18 node(){ 19 flag=0; 20 id=0; 21 memset(next,0,sizeof(next)); 22 } 23 }; 24 25 26 void insert(node *root,char *s,int id){ //插入 27 int i=0; 28 node *p=root; 29 while(s[i]){ 30 if(!p->next[(int)s[i]]){ 31 p->next[(int)s[i]]=new node; 32 p=p->next[(int)s[i]]; 33 } 34 else p=p->next[(int)s[i]]; 35 i++; 36 } 37 p->id=id; 38 p->flag=1; 39 } 40 41 void get_fail(node *root){ //设置tree 的fail指针 42 node *p, *tmp; 43 queue<node*>Q; 44 Q.push(root); 45 int i; 46 root->fail=NULL; 47 while(!Q.empty()){ 48 p=Q.front(); 49 Q.pop(); 50 for(i=0;i<130;i++){ 51 if(p->next[i]){ 52 if(p==root) p->next[i]->fail=root; 53 else{ 54 tmp=p->fail; 55 while(tmp){ 56 if(tmp->next[i]){ 57 p->next[i]->fail=tmp->next[i]; 58 break; 59 } 60 tmp=tmp->fail; 61 } 62 if(tmp==NULL) p->next[i]->fail=root; 63 } 64 Q.push(p->next[i]); 65 } 66 } 67 } 68 } 69 70 71 void solve(node *root,char *s,int k){ //解决问题 72 int j=0, i; 73 node *p=root, *tmp; 74 while(s[j]){ 75 i=s[j]; 76 while(!p->next[i]&&p!=root) p=p->fail; 77 p=p->next[i]; 78 if(p==NULL) p=root; 79 tmp=p; 80 while(tmp->flag&&tmp!=root&&!visited[tmp->id]){ 81 visited[tmp->id]=1; 82 ve[k].push_back(tmp->id); 83 tmp=tmp->fail; 84 } 85 j++; 86 } 87 88 } 89 main() 90 { 91 int t1, t2, i, j, k; 92 cin>>t1; 93 node *root=new node; 94 for(i=1;i<=t1;i++) 95 { 96 scanf("%s",a); 97 insert(root,a,i); 98 } 99 get_fail(root); 100 cin>>t2; 101 int t=t2;k=1; 102 while(t2--){ 103 memset(visited,0,sizeof(visited)); 104 scanf("%s",str); 105 solve(root,str,k);k++; 106 } 107 108 int num=0; 109 for(i=1;i<=t;i++){ 110 111 if(!ve[i].empty()){ 112 sort(ve[i].begin(),ve[i].end()); 113 printf("web %d:",i); 114 for(j=0;j<ve[i].size();j++) 115 printf(" %d",ve[i][j]); 116 cout<<endl; 117 num++; 118 } 119 } 120 printf("total: %d\n",num); 121 }
HDU 2896 AC自动机 + 细心,布布扣,bubuko.com
标签:des style blog java color strong
原文地址:http://www.cnblogs.com/qq1012662902/p/3854303.html