统计难题 HDOJ--2222

时间:2020-12-16 00:13:28

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29287    Accepted Submission(s): 9572

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
思路: AC自动机
AC代码:
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Node_Tree
{
int cnt;
struct Node_Tree *child[];
struct Node_Tree *fail;
}Node;
Node *root;
char keywd[];
char decpt[];
Node *q[];
int tail = , head = ;
void insert()
{
if(keywd == NULL)
return ;
int i;
char *p = keywd;
Node *t = root;
while(*p != '\0')
{
if(t->child[*p - 'a'] == NULL)
{
Node *temp = (Node *)malloc(sizeof(Node));
memset(temp, , sizeof(Node));
for(i = ; i < ; i ++)
{
temp->child[i] = NULL;
}
temp->cnt = ;
temp->fail = NULL;
t->child[*p - 'a'] = temp;
}
t = t->child[*p - 'a'];
p ++;
}
t->cnt ++;
} void getfail()
{
int i;
q[tail++] = root;
while(tail != head) //BFS;
{
Node *p = q[head++];
Node *temp = NULL;
for(i = ; i < ; i ++)
{
if(p->child[i] != NULL)
{
if(p == root)
{
p->child[i]->fail = root;
}
else
{
temp = p->fail;
while(temp != NULL)
{
if(temp->child[i] != NULL)
{
p->child[i]->fail = temp->child[i];
break ;
}
temp = temp->fail;
}
if(temp == NULL)
p->child[i]->fail = root;
}
q[tail++] = p->child[i];
}
}
}
} int search()
{
int i, ret = ;
char *p = decpt;
Node *t = root;
while(*p != '\0')
{
while(t->child[*p - 'a'] == NULL && t != root)
t = t->fail;
t = t->child[*p - 'a'];
if(t == NULL)
t = root;
Node *temp = t;
while(temp != root && temp->cnt != -)
{
ret += temp->cnt;
temp->cnt = -;
temp = temp->fail;
}
p ++;
}
return ret;
} int main(int argc, char const *argv[])
{
int c, i, t;
scanf("%d", &c);
Node TREEROOT;
root = &TREEROOT;
while(c --)
{
for(i = ; i < ; i ++)
{
root->child[i] = NULL;
root->cnt = ;
root->fail = NULL;
}
tail = head = ;
memset(decpt, , sizeof(decpt));
memset(keywd, , sizeof(keywd));
scanf("%d", &t);
while(t --)
{
scanf("%s", keywd);
insert();
memset(keywd, , sizeof(keywd));
}
getfail();
scanf("%s", decpt);
printf("%d\n", search());
}
return ;
}