標籤:contain efi tar void bre user com careful turn
Problem DescriptionIn 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.
InputFirst 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.
OutputPrint how many keywords are contained in the description.
Sample Input15shehesayshrheryasherhs
Sample Output3
AuthorWiskey
Recommendlcy | We have carefully selected several similar problems for you: 2896 3065 2243 2825 3341 每個範例給n個短字串,和一個長字串,問這個長字串中有幾個短字串出現過,ac自動機模板題。代碼:
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define MAX 1000000using namespace std;struct Trie { Trie *Next[26],*Fail; int sum; Trie() { for(int i = 0;i < 26;i ++) { Next[i] = NULL; } Fail = NULL; sum = 0; }}*root;void Insert_Str(char *s) {///字串插入到字典樹中 Trie *r = root; int i = -1; while(s[++ i]) { int d = s[i] - ‘a‘; if(r -> Next[d] == NULL) { r -> Next[d] = new Trie(); } r = r -> Next[d]; } r -> sum ++;///結尾加1}void Build_Fail() {///通過父結點的Fail更新子結點的Fail Trie *node,*temp; queue<Trie *> q; q.push(root); while(!q.empty()) { node = q.front(); q.pop(); for(int i = 0;i < 26;i ++) { if(node -> Next[i]) {///第i個兒子存在 temp = node -> Fail;///temp賦值當前節點的Fail while(temp) { if(temp -> Next[i]) { node -> Next[i] -> Fail = temp -> Next[i]; break; } temp = temp -> Fail; } if(temp == NULL) {///沒找到或者本來就是根節點 node -> Next[i] -> Fail = root; } q.push(node -> Next[i]); } } }}int Ac_automation(char *s) { int i = -1,ans = 0; Trie *node = root,*temp; while(s[++ i]) { int d = s[i] - ‘a‘; while(node != root && node -> Next[d] == NULL) node = node -> Fail;///如果沒有匹配的子結點 就找它的Fail看看有沒有匹配的子結點 if(node -> Next[d]) node = node -> Next[d]; temp = node; while(temp && temp -> sum >= 0) { ans += temp -> sum; temp -> sum = -1;///出現了 再出現時不再計算 temp = temp -> Fail;///找最長尾碼 } } return ans;}int main() { int t,n; char tr[50],s[MAX]; scanf("%d",&t); while(t --) { root = new Trie(); scanf("%d",&n); for(int i = 0;i < n;i ++) { scanf("%s",tr); Insert_Str(tr); } Build_Fail(); scanf("%s",s); printf("%d\n",Ac_automation(s)); }}
hdu 2222 Keywords Search