1. 程式人生 > >luogu P3796【模板】AC自動機(加強版)

luogu P3796【模板】AC自動機(加強版)

esp isdigit std tar printf show .org tor for

嘟嘟嘟

這個和某谷的AC自動機模板簡單版差不多。

但還是要註意幾點的:

1.這個是統計出現次數,而不是是否出現,所以在查詢的時候加上這個節點的val後,不能把val標記為-1。那麽也就可以說查詢的時間復雜度能比簡單版的稍微第一慢一點。

2.考慮k個一樣的模式串:剛開始我想的是每一個節點開一個vector,記錄這裏是第幾個模式串。但其實沒有這個必要,對於相同的模式串,我們只用記錄任意一個就行,反而在出現次數上要都加上。因為如果主串中存在這些相同的模式串,那麽出現次數應該是出現次數 * k。輸出的時候如果是這些串最多,那麽都應該把這些輸出。

技術分享圖片
  1 #include<cstdio>
  2
#include<iostream> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cctype> 8 #include<stack> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 #define enter puts("") 13 #define
space putchar(‘ ‘) 14 #define Mem(a, x) memset(a, x, sizeof(a)) 15 #define rg register 16 typedef long long ll; 17 typedef double db; 18 const int INF = 0x3f3f3f3f; 19 const db eps = 1e-8; 20 const int maxn = 1e6 + 5; 21 const int maxm = 1.05e4 + 5; 22 inline ll read() 23 { 24 ll ans = 0;
25 char ch = getchar(), las = ; 26 while(!isdigit(ch)) las = ch, ch = getchar(); 27 while(isdigit(ch)) ans = ans * 10 + ch - 0, ch = getchar(); 28 if(las == -) ans = -ans; 29 return ans; 30 } 31 inline void write(ll x) 32 { 33 if(x < 0) putchar(-), x = -x; 34 if(x >= 10) write(x / 10); 35 putchar(x % 10 + 0); 36 } 37 38 int n; 39 char s[maxn], ss[155][75]; 40 41 int sum[155]; 42 int ch[maxm][26], val[maxm], pos[maxm], f[maxm], cnt = 0; 43 int getnum(char c) 44 { 45 return c - a; 46 } 47 void insert(int id, char *s) 48 { 49 int m = strlen(s); 50 int now = 0; 51 for(int i = 0; i < m; ++i) 52 { 53 int c = getnum(s[i]); 54 if(!ch[now][c]) ch[now][c] = ++cnt; 55 now = ch[now][c]; 56 } 57 val[now]++; pos[now] = id; 58 } 59 void build() 60 { 61 queue<int> q; 62 for(int i = 0; i < 26; ++i) if(ch[0][i]) q.push(ch[0][i]); 63 while(!q.empty()) 64 { 65 int now = q.front(); q.pop(); 66 for(int i = 0; i < 26; ++i) 67 { 68 if(ch[now][i]) f[ch[now][i]] = ch[f[now]][i], q.push(ch[now][i]); 69 else ch[now][i] = ch[f[now]][i]; 70 } 71 } 72 } 73 void query(char *s) 74 { 75 int m = strlen(s), now = 0; 76 for(int i = 0; i < m; ++i) 77 { 78 int c = getnum(s[i]); 79 now = ch[now][c]; 80 for(int j = now; j; j = f[j]) sum[pos[j]] += val[j]; 81 } 82 } 83 84 void init() 85 { 86 Mem(ch, 0); Mem(val, 0); Mem(pos, 0); Mem(f, 0); 87 Mem(sum, 0); 88 cnt = 0; 89 } 90 91 int main() 92 { 93 while(scanf("%d", &n) && n) 94 { 95 init(); 96 for(int i = 1; i <= n; ++i) 97 { 98 scanf("%s", ss[i]); 99 insert(i, ss[i]); 100 } 101 build(); 102 scanf("%s", s); 103 query(s); 104 int Max = -1; 105 for(int i = 1; i <= n; ++i) Max = max(Max, sum[i]); 106 write(Max); enter; 107 for(int i = 1; i <= n; ++i) if(sum[i] == Max) printf("%s\n", ss[i]); 108 } 109 return 0; 110 }
View Code

luogu P3796【模板】AC自動機(加強版)