Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 70366 Accepted Submission(s): 23954
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
Sample Output
3
用了两种方法一种是malloc, 一种直接开完所有的节点, 不知道为什么第二种方法反而比较费时间。
第一种:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int mx = 1e6 + 5;
char str[mx];
struct node{
int fail, cnt;
int next[26];
}tree[500100]; //少按了一个0
int tot;
void add(node &a){
a.cnt =0;
a.fail = -1; //-1才是null , 0是root
memset(a.next, -1, sizeof(a.next));
}
void build1(char* te){
int p ,q;
int root = 0;
int i, v, len =strlen(te);
for(i =0, p= root; i<len; i++){
v = te[i] - 'a';
if( tree[p].next[v] == -1){
q = ++tot;
add(tree[q]);
tree[p].next[v] = q;
}
p = tree[p].next[v];
}
tree[p].cnt++;
}
queue<int> q;
void build_ac( ){
int root = 0;
while(!q.empty()) q.pop();
q.push(root);
while(!q.empty()){
int p = 0;
int te = q.front();
q.pop();
for(int i = 0; i<26; i++){
if(tree[te].next[i] == -1)
continue;
if(te == root)
tree[tree[te].next[i]].fail = root;
else{
p = tree[te].fail;
while(p != -1){
if(tree[p].next[i] != -1){
tree[tree[te].next[i]].fail =tree[p].next[i];
break;
}
p = tree[p].fail;
}
if(p == -1)
tree[tree[te].next[i]].fail = root;
}
q.push(tree[te].next[i]);
}
}
}
int query(){
int i, v, count = 0,root =0;
int p = root;
int len = strlen(str);
for(int i = 0; i< len; i++){
v = str[i] - 'a';
while(tree[p].next[v] == -1 && p != root)
p = tree[p].fail;
p = tree[p].next[v];
if(p == -1)
p = root;
int te = p;
while(te != root){
if(tree[te].cnt >= 0){
count += tree[te].cnt;
tree[te].cnt = -1;
}
else
break;
te = tree[te].fail;
}
}
return count;
}
int main(){
int T, n;
//freopen("123.txt", "r", stdin);
char te[55];
scanf("%d", &T);
while(T--){
add(tree[0]); //节点初始化
scanf("%d", &n);
tot = 0;
while(n--){
scanf("%s", te);
build1(te);
}
scanf("%s", str);
build_ac();
printf("%d\n", query());
}
return 0;
}
第二种
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cstdlib>
using namespace std;
const int mx = 1e6 + 5;
char str[mx];
struct node{
node* fail;
node* next[26];
int cnt;
}*root;
void add(node* root){
root->cnt = 0;
root->fail = NULL;
for(int i = 0; i < 26; i++){
root->next[i] = NULL;
}
}
void build1(char* te){
node *p ,*q;
int i, v, len =strlen(te);
for(i =0, p= root; i<len; i++){
v = te[i] - 'a';
if( p->next[v] == NULL){
q = (struct node*)malloc(sizeof(node));
add(q);
p->next[v] = q;
}
p = p->next[v];
}
p->cnt++;
}
queue<node*> q;
void build_ac(node* root){
while(!q.empty()) q.pop();
q.push(root);
while(!q.empty()){
node* p = NULL;
node* te = q.front();
q.pop();
for(int i = 0; i<26; i++){
if(te->next[i] == NULL)
continue;
if(te == root)
te->next[i]->fail = root;
else{
p = te->fail;
while(p != NULL){
// cout<<"jin"<<endl;
if(p->next[i] != NULL){
te->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if(p == NULL)
te->next[i]->fail = root;
}
q.push(te->next[i]);
}
}
}
int query(node* root){
int i, v, count = 0;
node* p = root;
int len = strlen(str);
for(int i = 0; i< len; i++){
v = str[i] - 'a';
while( p->next[v] == NULL && p != root) //next[v] 错写成 next
p = p->fail;
p = p->next[v]; // v错写成 i
if(p == NULL )
p = root; //这里写错
node* te = p;
while(te != root){
if(te->cnt >= 0){
count += te->cnt;
te->cnt = -1;
}
else
break;
te = te->fail;
}
}
return count;
}
int main(){
int T, n;
char te[55];
//freopen("123.txt", "r", stdin);
scanf("%d", &T);
while(T--){
root=(struct node*)malloc(sizeof(node));
add(root);
scanf("%d", &n);
while(n--){
scanf("%s", te);
build1(te);
}
scanf("%s", str);
build_ac(root);
//cout<<query(root)<<endl;
printf("%d\n", query(root));
}
return 0;
}