标签:字符串 特征 异常 vector 相同 aac sub 不能 std
C/C++:
1 #include <map> 2 #include <queue> 3 #include <cmath> 4 #include <vector> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <climits> 9 #include <iostream> 10 #include <algorithm> 11 #define INF 0x3f3f3f3f 12 using namespace std; 13 const int MAX_N = 1e5 + 10, MAX_L = 1e4 + 10; 14 15 int n, flag, m, total, num[510]; 16 char ss[MAX_L]; 17 18 struct node 19 { 20 int next[128], fail, id; 21 } s [MAX_N]; 22 23 void my_insert(int x, char *S) 24 { 25 int now = 0; 26 for (int i = 0; S[i]; ++ i) 27 { 28 int c = S[i]; 29 if (s[now].next[c] == 0) 30 s[now].next[c] = flag ++; 31 now = s[now].next[c]; 32 } 33 s[now].id = x; 34 } 35 36 void getfail() 37 { 38 int now, temp, v; 39 queue <int> Q; 40 Q.push(0); 41 while (Q.size()) 42 { 43 now = Q.front(); 44 Q.pop(); 45 for (int i = 0; i < 128; ++ i) 46 { 47 if (v = s[now].next[i]) 48 { 49 if (now == 0) s[v].fail = 0; 50 else 51 { 52 temp = s[now].fail; 53 while (temp && s[temp].next[i] == 0) 54 temp = s[temp].fail; 55 s[v].fail = s[temp].next[i]; 56 } 57 Q.push(v); 58 } 59 } 60 } 61 } 62 63 bool ac(int x, char *S) 64 { 65 int cnt = 0, now = 0, v; 66 for (int i = 0; S[i]; ++ i) 67 { 68 char c = S[i]; 69 while (now && s[now].next[c] == 0) 70 now = s[now].fail; 71 v = now = s[now].next[c]; 72 while (s[v].id) 73 { 74 num[cnt ++] = s[v].id; 75 v = s[v].fail; 76 } 77 } 78 if (cnt != 0) 79 { 80 sort(num, num + cnt); 81 printf("web %d:", x); 82 for (int i = 0; i < cnt; ++ i) 83 printf(" %d", num[i]); 84 printf("\n"); 85 return true; 86 } 87 return false; 88 } 89 90 int main() 91 { 92 scanf("%d", &n); 93 flag = 1, total = 0; 94 for (int i = 1; i <= n; ++ i) 95 { 96 scanf("%s", ss); 97 my_insert(i, ss); 98 } 99 scanf("%d", &m); 100 getfail(); 101 for (int i = 1; i <= m; ++ i) 102 { 103 scanf("%s", ss); 104 if (ac(i, ss)) total ++; 105 } 106 printf("total: %d\n", total); 107 return 0; 108 }
标签:字符串 特征 异常 vector 相同 aac sub 不能 std
原文地址:https://www.cnblogs.com/GetcharZp/p/9541566.html