1. 程式人生 > >HDU 2222 Keywords Search (AC自動機模板題)

HDU 2222 Keywords Search (AC自動機模板題)

出現 tro spa 繼續 time int cas keyword arc

Keywords Search

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 67950 Accepted Submission(s): 22882


Problem Description In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

Input First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a‘-‘z‘, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.

Output Print how many keywords are contained in the description.

Sample Input 1 5 she he say shr her yasherhs

Sample Output 3

Author Wiskey

Recommend lcy | We have carefully selected several similar problems for you: 2896 3065 2243 2825 3341 題意是給出幾個模式串,給一個匹配串,問能匹配多少個模式串。 代碼如下:
//
====================== // HDU 2222 // 求目標串中出現了幾個模式串 //==================== #include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> #include <queue> using namespace std; struct Trie { int Next[500010][26];//26是這裏討論26個小寫字母的情況,根據情況修改 int fail[500010],end[500010];//end數組表示以該節點結尾的字符串的數量 int root,L;//L用來標記節點序號,以廣度優先展開的字典樹的序號 int newnode() //建立新節點 { for(int i = 0;i < 26;i++) Next[L][i] = -1; //將該節點的後繼節點域初始化 end[L++] = 0; return L-1; //返回當前節點編號 } void init() //初始化操作 { L = 0; root = newnode(); } void insert(char buf[]) { int len = strlen(buf); int now = root; for(int i = 0;i < len;i++) { if(Next[now][buf[i]-a] == -1) //如果未建立當前的後繼節點,建立新的節點 Next[now][buf[i]-a] = newnode(); now = Next[now][buf[i]-a]; } end[now]++;//以該節點結尾的字符串數量增加1 } void build() { queue<int>Q; //用廣度優先的方式,將樹層層展開 fail[root] = root; for(int i = 0;i < 26;i++) if(Next[root][i] == -1) Next[root][i] = root; else { fail[Next[root][i]] = root; Q.push(Next[root][i]); } while( !Q.empty() ) { int now = Q.front(); Q.pop(); for(int i = 0;i < 26;i++) if(Next[now][i] == -1) Next[now][i] = Next[fail[now]][i];//該段的最後一個節點匹配後,跳到擁有最大公共後綴的fail節點繼續匹配 else { fail[Next[now][i]]=Next[fail[now]][i];//當前節點的fail節點等於它前驅節點的fail節點的後繼節點 Q.push(Next[now][i]); } } } int query(char buf[]) { int len = strlen(buf); int now = root; int res = 0; for(int i = 0;i < len;i++) { now = Next[now][buf[i]-a]; int temp = now; while( temp != root ) { res += end[temp];//加上以當前節點結尾的字符串數 end[temp] = 0;//該題是防止計算重復的字符串 temp = fail[temp];//每次找最大公共後綴對應的fail節點 } } return res; } void debug() { for(int i = 0;i < L;i++) { printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]); for(int j = 0;j < 26;j++) printf("%2d",Next[i][j]); printf("]\n"); } } }; char buf[1000010]; Trie ac; int main() { int t,n,ans; scanf("%d",&t); while(t--) { ac.init(); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",buf); ac.insert(buf); } ac.build(); scanf("%s",buf); printf("%d\n",ac.query(buf)); } return 0; }

HDU 2222 Keywords Search (AC自動機模板題)