hdu2222 Keywords Search(ac自动机)

Keywords Search

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


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自动机是基于trie树的,所以一般开的空间很大,所以我们需要把它设置为全局,不然会爆栈的。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max(a,b) a>b?a:b

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int maxn = 1e6;
const int maxm = 1e6+10;
using namespace std;

inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}

struct AC{
    int next[500100][26],fail[500100],end[500100];
    int tot,rt;
    int createNode(){
        for(int i=0;i<26;++i){
            next[tot][i]=-1;
        }
        end[tot++]=0;
        return tot-1;
    }
    void init(){
        tot=0;
        rt=createNode();
    }
    void insert(char *str){
        int len=strlen(str);
        int cur=rt;
        for(int i=0;i<len;++i){
            if(next[cur][str[i]-'a']==-1){
                next[cur][str[i]-'a']=createNode();
            }
            cur=next[cur][str[i]-'a'];
        }
        end[cur]++;
    }
    void build(){
        queue<int> que;
        fail[rt]=rt;
        for(int i=0;i<26;++i){
            if(next[rt][i]==-1){
                next[rt][i]=rt; 
            }else{
                fail[next[rt][i]]=rt;
                que.push(next[rt][i]);
            }
        }
        while(!que.empty()){
            int cur=que.front();
            que.pop();
            for(int i=0;i<26;++i){
                if(next[cur][i]==-1){
                    next[cur][i]=next[fail[cur]][i];//找不到fail指针跳到指向下一个匹配位置
                }else{
                    fail[next[cur][i]]=next[fail[cur]][i];//
                    que.push(next[cur][i]);
                }
            }
        }
    }
    int query(char *str){
        int len=strlen(str);
        int cur=rt;
        int ans=0;
        for(int i=0;i<len;++i){
            cur=next[cur][str[i]-'a'];
            int tmp=cur;
            while(tmp!=rt){
                ans+=end[tmp];
                end[tmp]=0;
                tmp=fail[tmp];
            }
        }
        return ans;
    }
};
char str1[51];
char str2[1000100];
AC ac;
int main(){
    int Case,m;
    Case=read();
    while(Case--){
        ac.init();
        m=read();
        while(m--){
            scanf("%s",str1);
            ac.insert(str1);
        }
        ac.build();
        scanf("%s",str2);
        printf("%d\n",ac.query(str2));
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值