日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu 2896 病毒侵袭(AC自动机)

發(fā)布時間:2025/3/16 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 2896 病毒侵袭(AC自动机) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

病毒侵襲

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)

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

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

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

Sample Input 3 aaa bbb ccc 2 aaabbbccc bbaacc
Sample Output web 1: 1 2 3 total: 1 分析:解法很容易想到。用給出的模式串構建AC自動機,然后用web主串去匹配就行了。 要注意一點,一個病毒可能會在一個web網(wǎng)站中出現(xiàn)多次,例如病毒為ab,網(wǎng)站為ababab,其實這只包含了一種病毒。所以我用了set來存儲病毒編號。 #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; //是不是病毒單詞的結尾node() {for(int i = 0; i < kind; i++)next[i] = NULL;cnt = 0; //0表示不是單詞結尾fail = NULL;} }*q[200*N]; node *root; int head, tail; char web[M]; char vir[205]; set<int> id_set; //記錄每個網(wǎng)站的病毒編號,因為一個病毒可能會在一個網(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; }

總結

以上是生活随笔為你收集整理的hdu 2896 病毒侵袭(AC自动机)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。