Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 70290 Accepted Submission(s): 23917
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.
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.
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
5
she
he
say
shr
her
yasherhs
Sample Output
3
Author
Wiskey
Recommend
题意:给出一个字典和一个模式串,问模式串中出现几个字典中的单词
代码:
////#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
//#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
const int M = ;
queue<int> q;
vector<string> vec;
bool vis[N];
struct Trie {
int trieN;
int ch[N][M], val[N], fail[N];
void init() {
memset(vis,,sizeof(vis));
trieN = -;
newnode();
}
int newnode() {
memset(ch[++trieN], , sizeof(ch[]));
val[trieN] = fail[trieN] = ;
return trieN;
}
void insert(const string &str, int index) {
int cur = ;
for (int i = ;str[i];i++) {
int d = str[i] - 'a';
if (!ch[cur][d])
ch[cur][d] = newnode();
cur = ch[cur][d];
}
if (val[cur]) vis[index] = ;
else val[cur] = index;
}
void build() {
for (int i = ;i < M;i++) {
if (ch[][i])
q.push(ch[][i]);
}
while (!q.empty()) {
int cur = q.front(); q.pop();
for (int i = ;i < M;i++) {
int &next = ch[cur][i];
if (next) {
fail[next] = ch[fail[cur]][i];
q.push(next);
}
else next = ch[fail[cur]][i];
}
}
}
void query(const string &str) {
int cur = , tmp;
for (int i = ;str[i];i++) {
int d = str[i] - 'a';
tmp = cur = ch[cur][d];
while (tmp && ~val[tmp]) {
if (val[tmp] != -) vis[val[tmp]] = ;
val[tmp] = -;
tmp = fail[tmp];
}
}
}
}ac; int main(){
ios::sync_with_stdio(false);
cin.tie();
int t;
cin >> t;
while(t--){
ac.init(); //初始化
int n;
string str;
cin >> n;
vec.resize(n+);
for(int i = ;i <= n;i++){
cin >> vec[i];
ac.insert(vec[i],i);
}
ac.build();
cin>>str;
ac.query(str); //查询是否在str里出现过
int ans=;
for(int i = ;i <= n;i++){
if (vis[i]) ans++;
}
pi(ans);
}
return ;
}