标签:stream 输出 search content for chm ini ble 有一个
1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <math.h> 7 #include <queue> 8 #include <set> 9 10 #define INF 0x3f3f3f3f 11 #define pii pair<int,int> 12 using namespace std; 13 14 15 16 17 struct ac_automation{ 18 int next[210*500][128],fail[210*500],end[210*500]; 19 int root,L; 20 bool vis[510]; 21 int newnode(){ 22 for (int i=0;i<128;i++) 23 { 24 next[L][i] = -1; 25 } 26 end[L++] = 0; 27 return L-1; 28 } 29 void init(){ 30 L = 0; 31 root = newnode(); 32 } 33 void insert(char s[],int id){ 34 int len = strlen(s); 35 int now = root; 36 for (int i=0;i<len;i++){ 37 if (next[now][s[i]] == -1) 38 next[now][s[i]] = newnode(); 39 now = next[now][s[i]]; 40 } 41 end[now] = id; 42 } 43 void build(){ 44 queue<int > Q; 45 fail[root] = root; 46 for (int i=0;i<128;i++) 47 { 48 if (next[root][i] == -1) 49 next[root][i] = root; 50 else{ 51 fail[next[root][i]] = root; 52 Q.push(next[root][i]); 53 } 54 } 55 while (!Q.empty()){ 56 int now = Q.front(); 57 Q.pop(); 58 for (int i=0;i<128;i++){ 59 if (next[now][i] == -1) 60 next[now][i] = next[fail[now]][i]; 61 else{ 62 fail[next[now][i]] = next[fail[now]][i]; 63 Q.push(next[now][i]); 64 } 65 } 66 } 67 } 68 bool querry(char buf[],int n,int id){ 69 int len = strlen(buf); 70 int now = root; 71 bool flag = false; 72 memset(vis,false, sizeof(vis)); 73 for (int i=0;i<len;i++){ 74 now = next[now][buf[i]]; 75 int temp = now; 76 while (temp!=root){ 77 if (end[temp]!=0){ 78 vis[end[temp]] = true; 79 flag = true; 80 } 81 temp = fail[temp]; 82 } 83 } 84 if (!flag) 85 return false; 86 printf("web %d:",id); 87 for (int i=1;i<=n;i++) 88 { 89 if (vis[i]) 90 printf(" %d",i); 91 } 92 printf("\n"); 93 return true; 94 } 95 }; 96 ac_automation ac; 97 char buf[10010]; 98 int main(){ 99 int n,m; 100 while (scanf("%d",&n)!=EOF){ 101 ac.init(); 102 for (int i=1;i<=n;i++){ 103 scanf("%s",buf); 104 ac.insert(buf,i); 105 } 106 ac.build(); 107 int ans = 0; 108 scanf("%d",&m); 109 for (int i=1;i<=m;i++){ 110 scanf("%s",buf); 111 if (ac.querry(buf,n,i)) 112 ans++; 113 } 114 printf("total: %d\n",ans); 115 } 116 return 0; 117 }
标签:stream 输出 search content for chm ini ble 有一个
原文地址:https://www.cnblogs.com/-Ackerman/p/11324276.html