您的位置:首页 > 其它

HDU2222-Keywords Search

2017-11-12 15:40 429 查看


Keywords Search

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

                                                                                            Total Submission(s): 69549    Accepted Submission(s): 23586


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

 

题意:输入一组字符串,最后一行输入一个字符串,问前面的字符串在最后一行的字符串中出现了几个

解题思路:ac自动机

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

char ch[1000010];

struct Trie
{
int next[500010][26], fail[500010], cnt[500010];
int root, tot;
int newnode()
{
for (int i = 0; i < 26; i++) next[tot][i] = -1;
cnt[tot++] = 0;
return tot - 1;
}
void init()
{
tot = 0;
root = newnode();
}
void insert(char ch[])
{
int k = root;
for (int i = 0; ch[i]; i++)
{
if (next[k][ch[i] - 'a'] == -1) next[k][ch[i] - 'a'] = newnode();
k = next[k][ch[i] - 'a'];
}
cnt[k]++;
}
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 pre = q.front();
q.pop();
for (int i = 0; i < 26; i++)
{
if (next[pre][i] == -1) next[pre][i] = next[fail[pre]][i];
else
{
fail[next[pre][i]] = next[fail[pre]][i];
q.push(next[pre][i]);
}
}
}
}
int query(char ch[])
{
int k = root, ans = 0;
for (int i = 0; ch[i]; i++)
{
k = next[k][ch[i] - 'a'];
int temp = k;
while (temp != root)
{
ans += cnt[temp];
cnt[temp] = 0;
temp = fail[temp];
}
}
return ans;
}
void debug()
{
for (int i = 0; i < tot; i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], cnt[i]);
for (int j = 0; j < 26; j++) printf("%2d", next[i][j]);
printf("]\n");
}
}
}ac;

int main()
{
int t, n;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
ac.init();
for (int i = 0; i < n; i++)
{
scanf("%s", ch);
ac.insert(ch);
}
ac.build();
scanf("%s", ch);
printf("%d\n", ac.query(ch));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: