标签:const 次数 ted field panel 大写 mission 网站源码 []
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19553 Accepted Submission(s): 6409
题意:
还是n个病毒,现在是1个文本串。统计模式串在文本串中出现的次数。
思路:
这个不是统计个数,就不需要vis了。同样要注意文本串中有可能有除了字母以外的字符,son还是要开了130
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 //#include <bits/stdc++.h> 11 using namespace std; 12 typedef long long LL; 13 #define inf 0x7f7f7f7f 14 15 int m, n; 16 const int maxn = 1005; 17 const int maxlen = 2e6 + 5; 18 19 struct virus{ 20 char str[55]; 21 int id; 22 }vir[maxn]; 23 struct tree{ 24 int fail; 25 int son[130]; 26 int ed; 27 //bool vis; 28 }AC[maxlen]; 29 int tot = 0; 30 char s[maxlen]; 31 32 void build(char s[], int id) 33 { 34 int len = strlen(s); 35 int now = 0; 36 for(int i = 0; i < len; i++){ 37 if(AC[now].son[s[i]] == 0){ 38 AC[now].son[s[i]] = ++tot; 39 } 40 now = AC[now].son[s[i]]; 41 } 42 AC[now].ed = id; 43 } 44 45 void get_fail() 46 { 47 queue<int>que; 48 for(int i = 0; i < 130; i++){ 49 if(AC[0].son[i] != 0){ 50 AC[AC[0].son[i]].fail = 0; 51 que.push(AC[0].son[i]); 52 } 53 } 54 while(!que.empty()){ 55 int u = que.front(); 56 que.pop(); 57 for(int i = 0; i < 130; i++){ 58 if(AC[u].son[i] != 0){ 59 AC[AC[u].son[i]].fail = AC[AC[u].fail].son[i]; 60 que.push(AC[u].son[i]); 61 } 62 else{ 63 AC[u].son[i] = AC[AC[u].fail].son[i]; 64 } 65 } 66 } 67 } 68 69 int cnt[maxn]; 70 void AC_query(char s[]) 71 { 72 /*for(int i = 0; i <= tot; i++){ 73 AC[i].vis = false; 74 }*/ 75 int len = strlen(s); 76 int now = 0; 77 for(int i = 0; i < len; i++){ 78 now = AC[now].son[s[i]]; 79 for(int t = now; t; t = AC[t].fail){ 80 if(AC[t].ed != 0){ 81 cnt[vir[AC[t].ed].id]++; 82 //AC[t].vis = true; 83 } 84 } 85 } 86 } 87 88 int main() 89 { 90 while(scanf("%d", &n) != EOF){ 91 for(int i = 0; i <= tot; i++){ 92 AC[i].fail = 0; 93 //AC[i].vis = false; 94 AC[i].ed = 0; 95 for(int j = 0; j < 130; j++){ 96 AC[i].son[j] = 0; 97 } 98 } 99 tot = 0; 100 for(int i = 1; i <= n; i++){ 101 cnt[i] = 0; 102 scanf("%s", vir[i].str); 103 vir[i].id = i; 104 build(vir[i].str, vir[i].id); 105 } 106 AC[0].fail = 0; 107 get_fail(); 108 getchar(); 109 gets(s); 110 AC_query(s); 111 for(int i = 1; i <= n; i++){ 112 if(cnt[i]){ 113 printf("%s: %d\n", vir[i].str, cnt[vir[i].id]); 114 } 115 } 116 } 117 return 0; 118 }
标签:const 次数 ted field panel 大写 mission 网站源码 []
原文地址:https://www.cnblogs.com/wyboooo/p/9898256.html