zoukankan      html  css  js  c++  java
  • HDU 2222 Keywords Search(AC自动机的入门题)

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 18800    Accepted Submission(s): 6269


    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
     
    Author
    Wiskey
     
    Recommend
    lcy
     
     
     
    这题就是AC自动机。
    AC自动机参考:
     
     
     
    总结了下,AC自动机并不难哈~~~
    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    const int MAX=26;
    typedef struct Trie_Node
    {
        struct Trie_Node *fail;//失败指针
        struct Trie_Node *next[MAX];//26子节点
        int count;//单词最后一个结点的计数
    }Trie;
    Trie *Q[500010];//队列,用于bfs构造
    char keyword[55];
    char str[1000010];
    int head,tail;//队列的头和尾
    
    void insert(Trie *root,char *word)
    {
        Trie *p=root;
        int i=0;
        while(word[i]!='\0')
        {
            if(p->next[word[i]-'a']==NULL)
            {
                Trie *temp=new Trie;
                for(int j=0;j<MAX;j++)
                    temp->next[j]=NULL;
                temp->count=0;
                temp->fail=NULL;
                p->next[word[i]-'a']=temp;
            }
            p=p->next[word[i]-'a'];
            i++;
        }
        p->count++;
    }
    void build_ac(Trie *root)
    {
        root->fail=NULL;
        head=tail=0;
        Q[head++]=root;
        while(head!=tail)
        {
            Trie *temp=Q[tail++];
            Trie *p=NULL;
            for(int i=0;i<MAX;i++)
            {
                if(temp->next[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;
                    }
                    Q[head++]=temp->next[i];
                }
            }
        }
    }
    int query(Trie *root)
    {
        int i=0;
        int cnt=0;
        int len=strlen(str);
        Trie *p=root;
        int index;
        while(str[i])
        {
            index=str[i]-'a';
            while(p->next[index]==NULL&&p!=root)p=p->fail;
            p=p->next[index];
            if(p==NULL)p=root;
            Trie *temp=p;
            while(temp!=root)
            {
                cnt+=temp->count;
                temp->count=0;
                temp=temp->fail;
            }
            i++;
        }
        return cnt;
    }
    void del(Trie *root)
    {
        for(int i=0;i<MAX;i++)
          if(root->next[i]!=NULL)
            del(root->next[i]);
        free(root);
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int T;
        int n;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            Trie *root=new Trie;
            root->count=0;
            root->fail=NULL;
            for(int i=0;i<MAX;i++)
                  root->next[i]=NULL;
            for(int i=0;i<n;i++)
            {
                scanf("%s",&keyword);
                insert(root,keyword);
            }
            build_ac(root);
            scanf("%s",&str);
            printf("%d\n",query(root));
            del(root);//时间卡得紧就不删除空间
        }
        return 0;
    }
  • 相关阅读:
    C# 中 Struct 与 Class 的区别,以及两者的适用场合<转转转>
    <转>.NET Framework十年回顾 体积越小功能越强
    一个数组,下标从0到n,元素为从0到n的整数。判断其中是否有重复元素
    在博客园已经一年多时间了,今天开通博客了!
    C#RSA非对称加解密
    无法向会话状态服务器发出会话状态请求请。确保 ASP.NET State Service (ASP.NET 状态服务)已启动
    JavaScript判断浏览器类型及版本(转)
    SQL Server 海量导入数据
    数据库管理方面必知语句(问答)(转)
    JS实现的购物车
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2626646.html
Copyright © 2011-2022 走看看