hdu 2896 病毒侵袭(AC自动机)
生活随笔
收集整理的這篇文章主要介紹了
hdu 2896 病毒侵袭(AC自动机)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
病毒侵襲
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)Problem Description 當(dāng)太陽的光輝逐漸被月亮遮蔽,世界失去了光明,大地迎來最黑暗的時(shí)刻。。。。在這樣的時(shí)刻,人們卻異常興奮——我們能在有生之年看到500年一遇的世界奇觀,那是多么幸福的事兒啊~~
但網(wǎng)路上總有那么些網(wǎng)站,開始借著民眾的好奇心,打著介紹日食的旗號,大肆傳播病毒。小t不幸成為受害者之一。小t如此生氣,他決定要把世界上所有帶病毒的網(wǎng)站都找出來。當(dāng)然,誰都知道這是不可能的。小t卻執(zhí)意要完成這不能的任務(wù),他說:“子子孫孫無窮匱也!”(愚公后繼有人了)。
萬事開頭難,小t收集了好多病毒的特征碼,又收集了一批詭異網(wǎng)站的源碼,他想知道這些網(wǎng)站中哪些是有病毒的,又是帶了怎樣的病毒呢?順便還想知道他到底收集了多少帶病毒的網(wǎng)站。這時(shí)候他卻不知道何從下手了。所以想請大家?guī)蛶兔ΑP又是個(gè)急性子哦,所以解決問題越快越好哦~~
Input 第一行,一個(gè)整數(shù)N(1<=N<=500),表示病毒特征碼的個(gè)數(shù)。
接下來N行,每行表示一個(gè)病毒特征碼,特征碼字符串長度在20—200之間。
每個(gè)病毒都有一個(gè)編號,依此為1—N。
不同編號的病毒特征碼不會(huì)相同。
在這之后一行,有一個(gè)整數(shù)M(1<=M<=1000),表示網(wǎng)站數(shù)。
接下來M行,每行表示一個(gè)網(wǎng)站源碼,源碼字符串長度在7000—10000之間。
每個(gè)網(wǎng)站都有一個(gè)編號,依此為1—M。
以上字符串中字符都是ASCII碼可見字符(不包括回車)。
Output 依次按如下格式輸出按網(wǎng)站編號從小到大輸出,帶病毒的網(wǎng)站編號和包含病毒編號,每行一個(gè)含毒網(wǎng)站信息。
web 網(wǎng)站編號: 病毒編號 病毒編號 …
冒號后有一個(gè)空格,病毒編號按從小到大排列,兩個(gè)病毒編號之間用一個(gè)空格隔開,如果一個(gè)網(wǎng)站包含病毒,病毒數(shù)不會(huì)超過3個(gè)。
最后一行輸出統(tǒng)計(jì)信息,如下格式
total: 帶病毒網(wǎng)站數(shù)
冒號后有一個(gè)空格。
Sample Input 3 aaa bbb ccc 2 aaabbbccc bbaacc
Sample Output web 1: 1 2 3 total: 1 分析:解法很容易想到。用給出的模式串構(gòu)建AC自動(dòng)機(jī),然后用web主串去匹配就行了。 要注意一點(diǎn),一個(gè)病毒可能會(huì)在一個(gè)web網(wǎng)站中出現(xiàn)多次,例如病毒為ab,網(wǎng)站為ababab,其實(shí)這只包含了一種病毒。所以我用了set來存儲(chǔ)病毒編號。 #include<cstdio> #include<set> using namespace std;const int kind = 128; //有多少種字符 const int N = 505; const int M = 10005; struct node {node *next[kind];node *fail;int id;//病毒編號int cnt; //是不是病毒單詞的結(jié)尾node() {for(int i = 0; i < kind; i++)next[i] = NULL;cnt = 0; //0表示不是單詞結(jié)尾fail = NULL;} }*q[200*N]; node *root; int head, tail; char web[M]; char vir[205]; set<int> id_set; //記錄每個(gè)網(wǎng)站的病毒編號,因?yàn)橐粋€(gè)病毒可能會(huì)在一個(gè)網(wǎng)站出現(xiàn)多次,set可以直接去重 set<int> ::iterator it;void Insert(char *str, int id) {node *p = root;int i = 0, index;while(str[i]) {index = str[i] - ' ';if(p->next[index] == NULL)p->next[index] = new node();p = p->next[index];i++;}p->cnt++;p->id = id; } void build_ac_automation(node *root) {root->fail = NULL;q[tail++] = root;node *p = NULL;while(head < tail) {node *temp = q[head++];for(int i = 0; i < kind; i++) {if(temp->next[i] != NULL) {if(temp == root) temp->next[i]->fail = root;else {p = temp->fail;while(p != NULL) {if(p->next[i] != NULL) {temp->next[i]->fail = p->next[i];break;}p = p->fail;}if(p == NULL) temp->next[i]->fail = root;}q[tail++] = temp->next[i];}}} } int Query(char *str) {id_set.clear();int i = 0, index;node *p = root;while(str[i]) {index = str[i] - ' ';while(p->next[index] == NULL && p != root) p = p->fail;p = p->next[index];if(p == NULL) p = root;node *temp = p;while(temp != root && temp->cnt > 0) {id_set.insert(temp->id);temp = temp->fail;}i++;}return id_set.size(); }int main() {int m, n;while(~scanf("%d",&n)) {int total = 0;head = tail = 0;root = new node();for(int i = 1; i <= n; i++) {scanf("%s", vir);Insert(vir, i);}build_ac_automation(root);scanf("%d",&m);for(int i = 1; i <= m; i++){scanf("%s",web);int s = Query(web);if(s) {total++;printf("web %d:", i);for(it = id_set.begin(); it != id_set.end(); it++)printf(" %d", *it);printf("\n");}}printf("total: %d\n", total);}return 0; }
總結(jié)
以上是生活随笔為你收集整理的hdu 2896 病毒侵袭(AC自动机)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 花了一个月时间梳理了一下公司的微服务核心
- 下一篇: 面试官:你说对MySQL事务很熟?那我问