刷题HDU 2222 Keywords Search

Posted hongyj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了刷题HDU 2222 Keywords Search相关的知识,希望对你有一定的参考价值。

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

Description(CHN)

(n) 个模式串和一个文本串,求文本串中出现了多少个模式串

Solution

AC自动机裸题,做法跟【刷题】洛谷 P3808 【模板】AC自动机(简单版)一模一样

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=10000+10,MAXM=1000000+10,MAXS=500000+10;
int T,n,ch[MAXS][30],fail[MAXS],ed[MAXS],cnt,vis[MAXS],last[MAXS],ps[MAXN];
char s[MAXM];
std::queue<int> q;
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void init()
{
    for(register int i=0,lt=strlen(s);i<lt;++i)s[i]-='a'-1;
}
inline void insert(int rk)
{
    int x=0;
    for(register int i=0,lt=strlen(s);i<lt;++i)
        if(!ch[x][s[i]])x=ch[x][s[i]]=++cnt;
        else x=ch[x][s[i]];
    ed[x]=1;
    ps[rk]=x;
}
inline void getfail()
{
    for(register int i=1;i<=26;++i)
        if(ch[0][i])q.push(ch[0][i]);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(register int i=1,y,z;i<=26;++i)
            if(ch[x][i])
            {
                y=ch[x][i],z=fail[x];
                while(z&&!ch[z][i])z=fail[z];
                fail[y]=ch[z][i];
                last[y]=ed[fail[y]]?fail[y]:last[fail[y]];
                q.push(y);
            }
            else ch[x][i]=ch[fail[x]][i];
    }
}
inline void save(int x)
{
    if(x)
    {
        if(ed[x])vis[x]=1;
        save(last[x]);
    }
}
inline void match()
{
    int x=0;
    for(register int i=0,lt=strlen(s);i<lt;++i)x=ch[x][s[i]],save(ed[x]||last[x]?x:0);
}
int main()
{
    read(T);
    while(T--)
    {
        memset(ch,0,sizeof(ch));
        memset(ed,0,sizeof(ed));
        memset(ps,0,sizeof(ps));
        memset(vis,0,sizeof(vis));
        memset(fail,0,sizeof(fail));
        memset(last,0,sizeof(last));
        cnt=0;
        read(n);
        for(register int i=1;i<=n;++i)scanf("%s",s),init(),insert(i);
        getfail();
        scanf("%s",s);init();match();
        int ans=0;
        for(register int i=1;i<=n;++i)
            if(vis[ps[i]])ans++;
        write(ans,'
');
    }
    return 0;
}

以上是关于刷题HDU 2222 Keywords Search的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2222 Keywords Search(瞎搞)

HDU 2222 - Keywords Search

hdu[2222]keywords search

hdu 2222 Keywords Search

HDU - 2222 Keywords Search

hdu 2222 Keywords Search