1. 程式人生 > >病毒侵襲 HDU - 2896(ac自動機 板題)

病毒侵襲 HDU - 2896(ac自動機 板題)

ffffff sync lap bre == 信息 bbb 冒號 nbsp

當太陽的光輝逐漸被月亮遮蔽,世界失去了光明,大地迎來最黑暗的時刻。。。。在這樣的時刻,人們卻異常興奮——我們能在有生之年看到500年一遇的世界奇觀,那是多麽幸福的事兒啊~~
但網路上總有那麽些網站,開始借著民眾的好奇心,打著介紹日食的旗號,大肆傳播病毒。小t不幸成為受害者之一。小t如此生氣,他決定要把世界上所有帶病毒的網站都找出來。當然,誰都知道這是不可能的。小t卻執意要完成這不能的任務,他說:“子子孫孫無窮匱也!”(愚公後繼有人了)。
萬事開頭難,小t收集了好多病毒的特征碼,又收集了一批詭異網站的源碼,他想知道這些網站中哪些是有病毒的,又是帶了怎樣的病毒呢?順便還想知道他到底收集了多少帶病毒的網站。這時候他卻不知道何從下手了。所以想請大家幫幫忙。小t又是個急性子哦,所以解決問題越快越好哦~~

Input第一行,一個整數N(1<=N<=500),表示病毒特征碼的個數。
接下來N行,每行表示一個病毒特征碼,特征碼字符串長度在20—200之間。
每個病毒都有一個編號,依此為1—N。
不同編號的病毒特征碼不會相同。
在這之後一行,有一個整數M(1<=M<=1000),表示網站數。
接下來M行,每行表示一個網站源碼,源碼字符串長度在7000—10000之間。
每個網站都有一個編號,依此為1—M。
以上字符串中字符都是ASCII碼可見字符(不包括回車)。


Output依次按如下格式輸出按網站編號從小到大輸出,帶病毒的網站編號和包含病毒編號,每行一個含毒網站信息。
web 網站編號: 病毒編號 病毒編號 …
冒號後有一個空格,病毒編號按從小到大排列,兩個病毒編號之間用一個空格隔開,如果一個網站包含病毒,病毒數不會超過3個。
最後一行輸出統計信息,如下格式
total: 帶病毒網站數
冒號後有一個空格。
Sample Input

3
aaa
bbb
ccc
2
aaabbbccc
bbaacc

Sample Output

web 1: 1 2 3
total: 1

#include <iostream>
#include <cstdio>
#include 
<sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define
lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 100100, INF = 0x7fffffff; int tot, id; queue<int> q; vector<int> v; struct node { int next[128]; int fail, cnt; }trie[maxn]; void init() { while(!q.empty()) q.pop(); for(int i=0; i<maxn; i++) { mem(trie[i].next, 0); trie[i].cnt = trie[i].fail = 0; } tot = id = 0; } void insert(char *s) { int n = strlen(s); int rt = 0; for(int i=0; i<n; i++) { int x = s[i]; if(trie[rt].next[x] == 0) trie[rt].next[x] = ++tot; rt = trie[rt].next[x]; } trie[rt].cnt = ++id; } void build() { trie[0].fail = -1; q.push(0); while(!q.empty()) { int u = q.front(); q.pop(); for(int i=0; i<128; i++) { if(trie[u].next[i]) { if(u == 0) trie[trie[u].next[i]].fail = 0; else { int v = trie[u].fail; while(v != -1) { if(trie[v].next[i]) { trie[trie[u].next[i]].fail = trie[v].next[i]; break; } v = trie[v].fail; } if(v == -1) trie[trie[u].next[i]].fail = 0; } q.push(trie[u].next[i]); } } } } void Get(int u) { while(u) { if(trie[u].cnt) v.push_back(trie[u].cnt); u = trie[u].fail; } } bool match(char *s, int k) { int n = strlen(s); int rt = 0; for(int i=0; i<n; i++) { int x = s[i]; if(trie[rt].next[x]) rt = trie[rt].next[x]; else { int p = trie[rt].fail; while(p != -1 && trie[p].next[x] == 0) p = trie[p].fail; if(p == -1) rt = 0; else rt = trie[p].next[x]; } // if(trie[rt].cnt) Get(rt); } if(!v.empty()) { sort(v.begin(), v.end()); // for(int i=0; i<v.size(); i++) // cout<< v[i] <<endl; v.erase(unique(v.begin(), v.end()), v.end()); printf("web %d:", k); for(int i=0; i<v.size(); i++) printf(" %d", v[i]); printf("\n"); return true; } return false; } int n, m, res = 0; char s[maxn]; int main() { scanf("%d", &n); rap(i, 1, n) { scanf("%s", s); insert(s); } build(); scanf("%d", &m); rap(i, 1, m) { v.clear(); scanf("%s", s); if(match(s, i)) res++; } printf("total: %d\n", res); return 0; }

病毒侵襲 HDU - 2896(ac自動機 板題)