hdu 2222 Keywords Search(AC自动机模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222

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

 

题目大意:t组数据,n个单词,一个文本串,问有多少个单词在文本串中出现过

题目思路:AC自动机模板题,板子就行

数组代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e6+5;

const int MAXN = 5e5+5;
#define son_num 26    //注意修改
struct Trie{
    int tree[MAXN][son_num];  //26是讨论全小写字母情况,根据题意修改
    int fail[MAXN];   //fail指针,匹配失败时返回位置
    int cnt[MAXN];    //cnt数组表示以该节点结束的字符串数量
    int root,tot;     //root是根节点,tot标记节点序号

    int newnode(){
        for(int i=0;iq;    //bfs寻找
        fail[root] = root;  //根节点的fail直接指向自己
        for(int i=0;i

指针代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 20071027;
const int N = 1e4+5;

int ans[10];
int vis[505];

#define son_num 26
//#define maxn 10010
struct node{
    int terminal;   //结束位置数量
    node *fail;
    node *Next[son_num];
    node(){
        fail=NULL;
        terminal=0;    //记录结束位置
        for(int i=0;iNext[index]==NULL)
            p->Next[index]=new node();
        p=p->Next[index];
    }
    p->terminal++;
}

//寻找失败指针
void build_fail(node *root){
    queue  que;
    root->fail=NULL;
    que.push(root);
    while(!que.empty()){
        node *temp=que.front();
        que.pop();
        node *p=NULL;
        for(int i=0;iNext[i]!=NULL){
                if(temp==root) temp->Next[i]->fail=root;
                else{
                    p=temp->fail;
                    while(p!=NULL){
                        if(p->Next[i]!=NULL){
                            temp->Next[i]->fail=p->Next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL)
                        temp->Next[i]->fail=root;
                }
                que.push(temp->Next[i]);
            }
        }
    }
}

//询问主串中含有多少个关键字
int query(node *root,char *str){
    int cnt=0;
    int len=strlen(str);
    node *p=root;
    for(int i=0;iNext[index]==NULL&&p!=root)
            p=p->fail;
        p=p->Next[index];
        if(p==NULL) p=root;
        node *temp=p;
        while(temp!=root){
            cnt+=temp->terminal;
            temp->terminal = 0;     //防止重复加字符串
            temp=temp->fail;

        }
    }
    return cnt;
}

//指针处理
void deal(node *now){
    if(now == NULL)
        return ;
    for(int i=0;iNext[i]!=NULL)
            deal(now->Next[i]);
    }
    delete now;
}

char s[1000005];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int n,m,t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        node *root = new node();
        for(int i=1;i<=n;i++){
            scanf(" %s",s);
            Insert(root,s);
        }
        build_fail(root);
        int x = 0;
        scanf(" %s",s);
        int tot = query(root,s);
        printf("%d\n",tot);
        deal(root);
    }
    return 0;
}

 

你可能感兴趣的:(AC自动机,hdu题解)