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

    Keywords Search

    Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/131072K (Java/Other)
    Total Submission(s) : 13 Accepted Submission(s) : 12
    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
    
    #include<iostream>
    #include<stdio.h>
    #include<string>
    #include<string.h>
    #include<queue>
    #include<stdlib.h>
    using namespace std;
    
    const int INF=26;///26个孩子
    char t[60],s[1000005];///t存模式串,s存文本串
    int ans;///统计模式串在文本串中匹配的个数
    
    struct Node{
        struct Node *son[INF];///节点的孩子节点
        struct Node *fail;///节点的fail指针
        int num;///以该节点结尾的模式串的个数
    };
    
    Node *InitNode()///节点的初始化
    {
        Node *p=new Node;
        for(int i=0;i<INF;i++)
            p->son[i]=NULL;///将26个孩子初始化为NULL
        p->fail=NULL;
        p->num=0;
        return p;
    }
    
    void Insert(Node *root)///用模式串建字典树
    {
        Node *p=root;
        int k=0;
        while(t[k]!='')
        {
            int a=t[k]-'a';
            if(p->son[a]==NULL)
            {
                Node *temp=new Node;
                temp=InitNode();
                p->son[a]=temp;
            }
            p=p->son[a];
            k++;
        }
        p->num++;///以该节点结尾的模式串的数量
    }
    
    void Build(Node *root)///给各个节点找fail指针的指向
    {
       Node *p=root;
       queue<Node *> q;
       q.push(p);///先将root节点进队
       while(!q.empty())///队列不为空的情况
       {
           p=q.front();
           q.pop();
           for(int i=0;i<INF;i++)///遍历该节点的孩子们
           {
               if(p->son[i]!=NULL)///如果孩子节点i不为NULL
               {
                   if(p==root)///与根相连的节点fail指针都指向root
                        p->son[i]->fail=root;
                    else///如果节点不是root节点
                    {
                        Node *temp=p->fail;///让temp指向该节点的fail指针
                        while(temp!=NULL)///入果temp不为NULL,说明p不是root节点,只有root的fail指针是NULL
                        {
                            if(temp->son[i]!=NULL)/**这句话的理解是如果该节点的fail指针指的节点的孩子i不为NULL,
                            说明找到了和p节点的孩子一样的元素了**/
                            {
                                p->son[i]->fail=temp->son[i];///那么p的孩子i的fail指针就指向找倒符合条件的节点
                                break;///跳出,不用再往上找了
                            }
                            temp=temp->fail;///如果没有找到,则还要往上查找
                        }
                        if(temp==NULL)///如果temp为NULL,说明已经查到root节点了
                            p->son[i]->fail=root;///那么p的孩子节点i的fail指针就只能指向root了
                    }
                    q.push(p->son[i]);///p的孩子节点i的fail指针找完了,将其进队,再找它的孩子节点的fail指针
               }
           }
       }
    }
    
    void Find(Node *root)///查找文本串中能匹配到的模式串的个数
    {
        Node *p=root;
        int k=0;
        ans=0;
        while(s[k]!='')///遍历文本串
        {
            int a=s[k]-'a';
            while(p->son[a]==NULL&&p!=root)///这说明在该节点位置失配了
            {
                p=p->fail;///那么就去他的fail指针所指向的节点的位置
            }
            p=p->son[a];///查看fail指针所指向的节点的孩子是否能与之匹配
            if(p==NULL)///如果匹配不到就回到根节点
                p=root;
            Node *temp=p;///定义一个temp,用来统计匹配到模式串
            while(temp!=NULL&&temp->num!=-1)///如果该节点不是NULL,并且该节点没有被统计过num的值
            {
                ans+=temp->num;/**统计一下num的值,num只有在模式串的结尾字符的节点才会有值,如果
                该节点不是模式串的结尾,那么num是0**/
                temp->num=-1;///将统计过的节点的num标记为-1
                temp=temp->fail;///顺着fail指针往上查找
            }
            k++;
        }
        printf("%d
    ",ans);
    }
    
    void Free(Node *root)///释放我们建的字典树
    {
        if(root!=NULL)
        {
            for(int i=0;i<INF;i++)
                Free(root->son[i]);
        }
        free(root);
    }
    
    int main()
    {
        int c,n;
        scanf("%d",&c);///组数
        while(c--)
        {
            Node *root=new Node;///新建一个root节点
            root=InitNode();///对新建的节点初始化
            scanf("%d",&n);///模式串的个数
            for(int i=0;i<n;i++)
            {
                scanf("%s",t);
                Insert(root);///将模式串插入到字典树里
            }
            scanf("%s",s);///输入文本串
            Build(root);
            Find(root);
            Free(root);
        }
        return 0;
    }
  • 相关阅读:
    Python中的traceback模块
    硬链接和软连接的区别
    Python中的fileinput模块和tempfile模块
    SQL Relay
    使用python+cron对php状态进行定时监控
    给想学习asp小朋友的建议
    emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
    python非贪婪、多行匹配正则表达式例子[转载]
    python中eval, exec, execfile,和compile [转载]
    url extracting 未测试
  • 原文地址:https://www.cnblogs.com/nanfenggu/p/7899966.html
Copyright © 2011-2022 走看看