HDU2222Keywords Search [AC自动机]

Keywords Search
Time Limit: 1000MS
Memory Limit: 131072KB
64bit IO Format: %I64d & %I64u

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自动机AC掉就好了;
用了两种方式,感觉差不多,一视同仁应该要简单些,访问的时候应该也要快一些(不过并没有快多少);
需要注意的是,这里求的是出现过的单词个数,注意到可能会反复回溯到可以的点,即一个单词可能在语句中出现多次,所以要清零!在做题之前一定要注意!
普通版:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define clr(x) memset(x,0,sizeof(x))
using namespace std;
const int maxn=1e4*50+5;
int ch[maxn][30],inde,len,nxt[maxn],last[maxn],m,n;
int ans,sig[maxn];
char ov[1000005];
int calc(char c)
{
    return c-'a';
}
void insert()
{
    int u=0;
    for(int i=0;i<len;i++){
        if(!ch[u][calc(ov[i])])
            ch[u][calc(ov[i])]=++inde;
        u=ch[u][calc(ov[i])];
    }
    sig[u]++;
}
void getfail()
{
    queue<int>q;nxt[0]=0;
    for(int i=0;i<26;i++)if(ch[0][i]){
        int u=ch[0][i];
        nxt[u]=0;last[u]=0;q.push(u);
    }
    while(!q.empty()){
        int cur=q.front();q.pop();
        for(int i=0;i<26;i++)if(ch[cur][i]){
            int u=ch[cur][i];
            q.push(u);
            int v=nxt[cur];
            while(v&&!ch[v][i])v=nxt[v];
            nxt[u]=ch[v][i];
            last[u]=sig[nxt[u]]?nxt[u]:last[nxt[u]];
        }
    }
}
void init()
{
    scanf("%d",&m);inde=0;clr(ch);ans=0;clr(nxt);clr(last);clr(sig);
    for(int i=1;i<=m;i++){
        scanf("%s",ov);len=strlen(ov);
        insert();
    }
    getfail();
}
inline int count(int root)
{
    if(!root) return 0;
    int ret=count(last[root])+sig[root];
    sig[root]=0;
    return ret;
}

void mat()
{
    len=strlen(ov);int j=0;
    for(int i=0;i<len;i++){
        int u=calc(ov[i]);
        while(j&&!ch[j][u])j=nxt[j];
        j=ch[j][u];int tmp=j;
        if(sig[tmp]){
            ans+=sig[tmp];sig[tmp]=0;
            while(last[tmp]){
                tmp=last[tmp];ans+=sig[tmp];sig[tmp]=0;//可能会多次访问,
                //但却只计算一次(具体是否清零,以及是否直接++要看题意;
                //(是出现了多少次还是出现了多少个单词 //所以不用开longlong 
            }
        }
       /*if(sig[tmp]) ans+=count(tmp);
        else ans+=count(last[tmp]);*/
    }
    printf("%d\n",ans);
}
int main()
{
    freopen("hdu2222.in","r",stdin);
    freopen("hdu2222.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        init();
        scanf("%s",ov);
        mat();
    }
    return 0;
}

一视同仁版:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define clr(x) memset(x,0,sizeof(x))
using namespace std;
const int maxn=1e4*50+5;
int ch[maxn][30],inde,len,nxt[maxn],last[maxn],m,n;
int ans,sig[maxn];
char ov[1000005];
int calc(char c)
{
    return c-'a';
}
void insert()
{
    int u=0;
    for(int i=0;i<len;i++){
        if(!ch[u][calc(ov[i])])
            ch[u][calc(ov[i])]=++inde;
        u=ch[u][calc(ov[i])];
    }
    sig[u]++;
}
void getfail()
{
    queue<int>q;nxt[0]=0;
    for(int i=0;i<26;i++)if(ch[0][i]){
        int u=ch[0][i];
        nxt[u]=0;last[u]=0;q.push(u);
    }
    while(!q.empty()){
        int cur=q.front();q.pop();
        for(int i=0;i<26;i++)if(ch[cur][i]){
            int u=ch[cur][i];
            q.push(u);
            int v=nxt[cur];
            while(v&&!ch[v][i])v=nxt[v];
            nxt[u]=ch[v][i];
            last[u]=sig[nxt[u]]?nxt[u]:last[nxt[u]];
        }else ch[cur][i]=ch[nxt[cur]][i];
    }
}
void init()
{
    scanf("%d",&m);inde=0;clr(ch);ans=0;clr(nxt);clr(last);clr(sig);
    for(int i=1;i<=m;i++){
        scanf("%s",ov);len=strlen(ov);
        insert();
    }
    getfail();
}
inline int count(int root)
{
    if(!root) return 0;
    int ret=count(last[root])+sig[root];
    sig[root]=0;
    return ret;
}
void mat()
{
    len=strlen(ov);int j=0;
    for(int i=0;i<len;i++){
        int u=calc(ov[i]);
        j=ch[j][u];int tmp=j;
        if(sig[tmp]){
            ans+=sig[tmp];sig[tmp]=0;
            while(last[tmp]){
                tmp=last[tmp];ans+=sig[tmp];sig[tmp]=0;//可能会多次访问,
                //但却只计算一次(具体是否清零,以及是否直接++要看题意;
                //(是出现了多少次还是出现了多少个单词 
            }
        }
       /*if(sig[tmp]) ans+=count(tmp);
        else ans+=count(last[tmp]);*/
    }
    printf("%d\n",ans);
}
int main()
{
    freopen("hdu2222.in","r",stdin);
    freopen("hdu2222.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        init();
        scanf("%s",ov);
        mat();
    }
    return 0;
}

其实就是把缺的边全部补上以减少回去的次数;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值