您的位置:首页 > 其它

hdu2222-Keywords Search 【AC自动机】

2016-04-29 16:15 405 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2222

Keywords Search

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 49940 Accepted Submission(s): 16020


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
Print how many keywords are contained in the description.

[align=left]Sample Input[/align]

1
5
she
he
say
shr
her
yasherhs

[align=left]Sample Output[/align]

3

题意:多模式串匹配,AC自动机的入门版。求多个模式串在母串出现的总个数。
思路:参考http://www.cnblogs.com/xudong-bupt/p/3433506.html
三步走,第一步建立字段数,把模式串整合为一棵树;第二步最重要,为各节点初始化fail指针,bfs搜索,功能类似于kmp的next数组,root的fail指向null,第二层指向root,第三层以后,判断其父亲的fail是否有同样的儿子,有就fail指过去那个儿子,没有就找其父亲的fail的fail,循环至root,没办法,就指向root(见下图);第三步就是搜索,与第二步类似,沿着fail指针找就对了,具体看代码。



代码:

#include <fstream>
#include <iostream>
using namespace std;

#define MAXLEN 1000005
#define N 26
#define QUELEN 500005
#define SLEN 55

struct Node
{
Node *word
;
Node *fail;
int cnt;
Node (): cnt (0), fail(NULL)
{
memset (word, NULL, sizeof (word));
}
}*root;

int n;
char str[MAXLEN];

void Insert (char*);
void Init ();
void BuildACAutomation ();
int ACSearch (char*);

int main ()
{
//freopen("D:\\input.in","r",stdin);
int T;
scanf ("%d", &T);
while (T--)
{
Init ();
BuildACAutomation ();
scanf ("%s", str);
printf ("%d\n", ACSearch (str));
}
return 0;
}
void Insert (char *s)
{
Node *p = root;
int i = 0;
while (s[i])
{
int tmp = s[i] - 97;
if (!p->word[tmp])
{
p->word[tmp] = new Node;
}
p = p->word[tmp];
i++;
}
p->cnt++;
}
void Init ()
{
char s[SLEN];
root = new Node;
scanf ("%d", &n);
for (int i = 0; i < n; i++)
{
scanf ("%s", s);
Insert (s);
}
}
void BuildACAutomation ()
{
Node *que[QUELEN];
int front = 0, rear = 0;
for (int i = 0; i < N; i++)
{
if (root->word[i])//处理第二层,使其fail指针都指向root,而root的fail默认指向null
{
que[rear++] = root->word[i];
root->word[i]->fail = root;
}
}
while (front < rear)//处理其他层
{
Node *p = que[front++], *cur;
for (int i = 0; i < N; i++)
{
if (p->word[i])
{
que[rear++] = p->word[i];
cur = p->fail;
while (cur)
{
if (cur->word[i])
{
p->word[i]->fail = cur->word[i];
break;
}
cur = cur->fail;
}
if (!cur)
{
p->word[i]->fail = root;
}
}
}
}
}
int ACSearch (char *s)
{
int i = 0, ans = 0;
Node *p = root, *cur;
while (s[i])
{
int tmp = s[i] - 97;
while ((!p->word[tmp]) && p != root)
{
p = p->fail;
}
p = p->word[tmp];
if (!p)
{
p = root;
}
cur = p;
while (cur && (~cur->cnt))
{
ans += cur->cnt;
cur->cnt = -1;
cur = cur->fail;
}
i++;
}
return ans;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: