您的位置:首页 > 其它

[HDU 2222] Keywords Search (AC自动机)

2015-11-16 19:12 465 查看
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自动机中的精髓:

(1)fail指针(重要):由于一个串可能会包含另一个串,所以trie树上的非结尾点也可能是另一个单词的结尾。所以即使val==0的点也要fail 一下判断他是不是另一个单词的节点。

(2)fail的优化:用last 表示顺着fail回去能找到的第一个单词结尾。

(3)trie的优化(重要):对于不存在的nxt[ i ] ,存储它可能的fail。以节省在匹配的时候反复fail的时间。因为这个节点不存在,匹配到他的时候肯定失配了。 trie[ np ].nxt[ i ] = trie[ fap ].nxt[ i ]; 所以匹配的时候就不用while一下找fail了, 直接读这个点,若不存在,里边存的就是可能的fail。np = trie[ np ].nxt[ chn ]; 

这题也存在一些坑点:

(1)相同的字符串可能重复出现,最后计数的时候要按照不同的字符串处理。

(2)沿着fail 回去把那些val 累加起来后,别忘了清零,以防重复计数。

#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
const int noden = 10000*50+1000, inptn = 1000000+100;

int chnum(char a){return a-'a';}

struct trnode
{
char chr;
int nxt[28];
int fail;
int last;
int val;
};

int n;
char inpt[inptn];
trnode trie[noden];
int triep;

int que[noden];

void addstr(char*);
void calfail();

int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
triep = 0;
memset(trie, 0, sizeof(trie));
while(n--)
{
char patn[80];
scanf("%s", patn);
addstr(patn);
}
calfail();

scanf("%s", inpt);
int ilen = strlen(inpt);
int np = 0;
int ans = 0;

for(int i=0; i<ilen; i++)
{
int chn = chnum(inpt[i]);
np = trie[np].nxt[chn];
if(trie[np].val)
{
ans += trie[np].val;
trie[np].val = 0;
}
int temp = trie[np].last;
while(temp && trie[temp].val)
{
ans += trie[temp].val;
trie[temp].val = 0;
temp = trie[temp].last;
}

}

printf("%d\n", ans);

}
return 0;
}

void addstr( char *patn )
{
int plen = strlen(patn);
int np = 0;
for(int i=0; i<plen; i++)
{
if( trie[np].nxt[chnum(patn[i])] )
{
np = trie[np].nxt[ chnum(patn[i]) ];
}
else
{
triep++;
trie[np].nxt[ chnum(patn[i]) ] = triep;
np = triep;
trie[np].chr = patn[i];
}
if(i == plen-1) trie[np].val ++;
}
return;
}

void calfail()
{
int qhead=0, qtail=0;

for(int i=0; i<26; i++)
{
int nxp = trie[0].nxt[i];
if(nxp)
que[++qtail] = nxp;
}

while(qhead<qtail)
{
int np = que[++qhead];
for(int i=0; i<26; i++)
{
int nxp = trie[np].nxt[i];
int fap = trie[np].fail;
if(!nxp)
{
trie[np].nxt[i] = trie[fap].nxt[i];
continue;
}
que[++qtail] = nxp;
trie[nxp].fail = trie[fap].nxt[i];
fap = trie[nxp].fail;
trie[nxp].last = trie[fap].val ? fap: trie[fap].last;
}
}
return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: