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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

hdu 3065 病毒侵袭持续中(AC自动机)

發布時間:2025/3/16 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 3065 病毒侵袭持续中(AC自动机) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

病毒侵襲持續中

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

Problem Description 小t非常感謝大家幫忙解決了他的上一個問題。然而病毒侵襲持續中。在小t的不懈努力下,他發現了網路中的“萬惡之源”。這是一個龐大的病毒網站,他有著好多好多的病毒,但是這個網站包含的病毒很奇怪,這些病毒的特征碼很短,而且只包含“英文大寫字符”。當然小t好想好想為民除害,但是小t從來不打沒有準備的戰爭。知己知彼,百戰不殆,小t首先要做的是知道這個病毒網站特征:包含多少不同的病毒,每種病毒出現了多少次。大家能再幫幫他嗎?
Input 第一行,一個整數N(1<=N<=1000),表示病毒特征碼的個數。
接下來N行,每行表示一個病毒特征碼,特征碼字符串長度在1—50之間,并且只包含“英文大寫字符”。任意兩個病毒特征碼,不會完全相同。
在這之后一行,表示“萬惡之源”網站源碼,源碼字符串長度在2000000之內。字符串中字符都是ASCII碼可見字符(不包括回車)。

Output 按以下格式每行一個,輸出每個病毒出現次數。未出現的病毒不需要輸出。
病毒特征碼: 出現次數
冒號后有一個空格,按病毒特征碼的輸入順序進行輸出。

Sample Input 3 AA BB CC ooxxCC%dAAAoen....END
Sample Output AA: 2 CC: 1Hint Hit: 題目描述中沒有被提及的所有情況都應該進行考慮。比如兩個病毒特征碼可能有相互包含或者有重疊的特征碼段。 計數策略也可一定程度上從Sample中推測。 分析:這題和hdu 2896差不多,只是多輸出了每個病毒出現的次數,用一個cnt數組記錄一下次數即可。 #include<cstdio> #include<cstring> using namespace std;const int kind = 26; //有多少種字符 const int N = 1005; const int M = 2000005; struct node {node *next[kind];node *fail;int id;//病毒編號node() {for(int i = 0; i < kind; i++)next[i] = NULL;fail = NULL;id = 0;} }*q[51*N]; node *root; int head, tail; char source[M], s[M]; char vir[N][55]; int cnt[N];void Insert(char *str, int id) {node *p = root;int i = 0, index;while(str[i]) {index = str[i] - 'A';if(p->next[index] == NULL)p->next[index] = new node();p = p->next[index];i++;}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];}}} } void Query(char *str) {int i = 0, index;node *p = root;while(str[i]) {index = str[i] - 'A';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->id > 0) {cnt[temp->id]++;temp = temp->fail;}i++;} }int main() {int n;while(~scanf("%d",&n)) {memset(cnt, 0, sizeof(cnt));head = tail = 0;root = new node();for(int i = 1; i <= n; i++) {scanf("%s", vir[i]);Insert(vir[i], i);}build_ac_automation(root);scanf("%s",source);int len = strlen(source);int l = 0;for(int i = 0; i <= len; i++) {if(source[i] >= 'A' && source[i] <= 'Z') //因為病毒中只有大寫字母,所以只處理連續的大寫字母組成的字符串s[l++] = source[i];else {s[l] = '\0';Query(s);l = 0;}}for(int i = 1; i <= n; i++)if(cnt[i])printf("%s: %d\n",vir[i], cnt[i]);}return 0; }

總結

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

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