zoukankan      html  css  js  c++  java
  • HDU 2222 Keywords Search(AC自动机模版题)

    Keywords Search

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


    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
     
     
    题意:第一行输入测试数据的组数,然后输入一个整数n,接下来的n行每行输入一个单词,最后输入一个字符串,问在这个字符串中有多少个单词出现过。

    解题思路:这是一道多模式串的字符匹配问题,又名AC自动机,听着名字好高大上的赶脚。KMP处理的是单模式串匹配,其实KMP也能处理多模式串匹配的问题,不过当数据很大的时候,是相当的耗费时间,所以就有前辈们发明了AC自动机这个高效的多模式串匹配算法。

    AC自动机算法及模板

    http://blog.csdn.net/liu940204/article/details/51347064

      1 #include <stdio.h>  
      2 #include <stdlib.h>  
      3 #include <string.h>  
      4 struct Node
      5 {
      6     int cnt;//是否为该单词的最后一个结点   
      7     Node *fail;//失败指针   
      8     Node *next[26];//Trie中每个结点的各个节点   
      9 }*queue[500005];//队列,方便用BFS构造失败指针   
     10 char s[1000005];//主字符串   
     11 char keyword[55];//需要查找的单词   
     12 Node *root;//头结点   
     13 void Init(Node *root)//每个结点的初始化   
     14 {
     15     root->cnt = 0;
     16     root->fail = NULL;
     17     for (int i = 0; i<26; i++)
     18         root->next[i] = NULL;
     19 }
     20 
     21 void Build_trie(char *keyword)//构建Trie树   
     22 {
     23     Node *p, *q;
     24     int i, v;
     25     int len = strlen(keyword);
     26     for (i = 0, p = root; i<len; i++)
     27     {
     28         v = keyword[i] - 'a';
     29         if (p->next[v] == NULL)
     30         {
     31             q = (struct Node *)malloc(sizeof(Node));
     32             Init(q);
     33             p->next[v] = q;//结点链接   
     34         }
     35         p = p->next[v];//指针移动到下一个结点   
     36     }
     37     p->cnt++;//单词最后一个结点cnt++,代表一个单词   
     38 }
     39 
     40 void Build_AC_automation(Node *root)
     41 {
     42     int head = 0, tail = 0;//队列头、尾指针   
     43     queue[head++] = root;//先将root入队   
     44     while (head != tail)
     45     {
     46         Node *p = NULL;
     47         Node *temp = queue[tail++];//弹出队头结点   
     48         for (int i = 0; i<26; i++)
     49         {
     50             if (temp->next[i] != NULL)//找到实际存在的字符结点   
     51             { //temp->next[i] 为该结点,temp为其父结点   
     52                 if (temp == root)//若是第一层中的字符结点,则把该结点的失败指针指向root   
     53                     temp->next[i]->fail = root;
     54                 else
     55                 {
     56                     //依次回溯该节点的父节点的失败指针直到某节点的next[i]与该节点相同,  
     57                     //则把该节点的失败指针指向该next[i]节点;   
     58                     //若回溯到 root 都没有找到,则该节点的失败指针指向 root  
     59                     p = temp->fail;//将该结点的父结点的失败指针给p   
     60                     while (p != NULL)
     61                     {
     62                         if (p->next[i] != NULL)
     63                         {
     64                             temp->next[i]->fail = p->next[i];
     65                             break;
     66                         }
     67                         p = p->fail;
     68                     }
     69                     //让该结点的失败指针也指向root   
     70                     if (p == NULL)
     71                         temp->next[i]->fail = root;
     72                 }
     73                 queue[head++] = temp->next[i];//每处理一个结点,都让该结点的所有孩子依次入队   
     74             }
     75         }
     76     }
     77 }
     78 int query(Node *root)
     79 { //i为主串指针,p为模式串指针   
     80     int i, v, count = 0;
     81     Node *p = root;
     82     int len = strlen(s);
     83     for (i = 0; i<len; i++)
     84     {
     85         v = s[i] - 'a';
     86         //由失败指针回溯查找,判断s[i]是否存在于Trie树中   
     87         while (p->next[v] == NULL && p != root)
     88             p = p->fail;
     89         p = p->next[v];//找到后p指针指向该结点   
     90         if (p == NULL)//若指针返回为空,则没有找到与之匹配的字符   
     91             p = root;
     92         Node *temp = p;//匹配该结点后,沿其失败指针回溯,判断其它结点是否匹配   
     93         while (temp != root)//匹配结束控制   
     94         {
     95             if (temp->cnt >= 0)//判断该结点是否被访问   
     96             {
     97                 count += temp->cnt;//由于cnt初始化为 0,所以只有cnt>0时才统计了单词的个数   
     98                 temp->cnt = -1;//标记已访问过   
     99             }
    100             else//结点已访问,退出循环   
    101                 break;
    102             temp = temp->fail;//回溯 失败指针 继续寻找下一个满足条件的结点   
    103         }
    104     }
    105     return count;
    106 }
    107 
    108 int main()
    109 {
    110     int T, n;
    111     scanf("%d", &T);
    112     while (T--)
    113     {
    114         root = (struct Node *)malloc(sizeof(Node));
    115         Init(root);
    116         scanf("%d", &n);
    117         for (int i = 0; i<n; i++)
    118         {
    119             scanf("
    %s", keyword);
    120             Build_trie(keyword);
    121         }
    122         Build_AC_automation(root);
    123         scanf("
    %s", s);
    124         printf("%d
    ", query(root));
    125     }
    126     return 0;
    127 }
      1 #include <iostream>  
      2 #include <cstdio>  
      3 #include <cstring>  
      4 #include <cmath>  
      5 #include <map>  
      6 #include <set>  
      7 #include <algorithm>  
      8 #include <ctime>  
      9 #include <vector>  
     10 #include <string>  
     11 #include <stack>  
     12 #include <queue>  
     13 using namespace std;
     14 #define maxnode 10000*100  
     15 struct Node
     16 {
     17     Node *fail;
     18     Node *nxt[26];
     19     int count;
     20     Node()
     21     {
     22         fail = NULL;
     23         memset(nxt, NULL, sizeof(nxt));
     24         count = 0;
     25     }
     26 }*q[maxnode];//队列,方便用BFS构造失败指针
     27 
     28 void insert(Node *root, char *str)
     29 {
     30     Node *p = root;
     31     for (int i = 0; str[i] != ''; ++i)
     32     {
     33         if (p->nxt[str[i] - 'a'] == NULL)
     34             p->nxt[str[i] - 'a'] = new Node();
     35         p = p->nxt[str[i] - 'a'];
     36     }
     37     p->count++;//以p结尾的单词个数
     38 }
     39 
     40 //在构造完这棵Tire之后,接下去的工作就是构造下失败指针。
     41 //构造失败指针的过程概括起来就一句话:设这个节点上的字母
     42 //为C,沿着他父亲的失败指针走,直到走到一个节点,他的儿子
     43 //中也有字母为C的节点。然后把当前节点的失败指针指向那个字
     44 //母也为C的儿子。如果一直走到了root都没找到,那就把失败指
     45 //针指向root。具体操作起来只需要:先把root加入队列(root的
     46 //失败指针指向自己或者NULL),这以后我们每处理一个点就把它
     47 //的所有儿子加入队列,队列为空。
     48 void getfail(Node *root)
     49 {
     50     Node *u, *t, *p;
     51     root->fail = NULL;
     52     int head = 0, tail = 0;//队列头、尾指针   
     53     q[tail++] = root;//先将root入队 
     54     while (head<tail)
     55     {
     56         u = q[head++];//弹出队头结点  
     57         for (int i = 0; i < 26; ++i)
     58         {
     59             if (u->nxt[i] != NULL)//找到实际存在的字符结点
     60             {//u->nxt[i] 为该结点,u为其父结点 
     61                 if (u == root)//若是第一层中的字符结点,则把该结点的失败指针指向root   
     62                     u->nxt[i]->fail = root;
     63                 else
     64                 { //依次回溯该节点的父节点的失败指针直到某节点的next[i]与该节点相同,  
     65                   //则把该节点的失败指针指向该next[i]节点;   
     66                   //若回溯到 root 都没有找到,则该节点的失败指针指向 root
     67                     p = u->fail;//将该结点的父结点的失败指针给p   
     68                     while (p != NULL)//失败指针不为空
     69                     {
     70                         if (p->nxt[i] != NULL)
     71                         {//他的儿子中也有字母为C的节点
     72                             u->nxt[i]->fail = p->nxt[i];//那么指向它
     73                             break;
     74                         }
     75                         p = p->fail;
     76                     }
     77                     if (p == NULL)
     78                         u->nxt[i]->fail = root; //让该结点的失败指针也指向root   
     79                 }
     80                 q[tail++] = u->nxt[i];//每处理一个结点,都让该结点的所有孩子依次入队   
     81             }
     82         }
     83     }
     84 }
     85 
     86 int query(Node *root, char *str)
     87 {
     88     int res = 0;
     89     Node *p = root, *temp;
     90     for (int i = 0; str[i] != ''; ++i)
     91     {
     92         Node *pre = p;
     93         while (p->nxt[str[i] - 'a'] == NULL && p != root)
     94         {
     95             p = p->fail;//到失败指针所指的地方
     96         }
     97         p = p->nxt[str[i] - 'a'];//继续查找
     98         if (p == NULL)
     99             p = root;
    100         //if(p!=root)  
    101         //p=p->nxt[str[i]-'a'];  
    102         temp = p;
    103         while (temp != NULL && temp->count != -1)
    104         {
    105             res += temp->count;//由于count初始化为 0,所以只有cnt>0时才统计了单词的个数   
    106             temp->count = -1;//标记已访问过   
    107             temp = temp->fail;//回溯 失败指针 继续寻找下一个满足条件的结点 
    108         }
    109     }
    110     return res;
    111 }
    112 
    113 char str[100];
    114 char T[1000500];
    115 int main()
    116 {
    117     int ncase, n;
    118     scanf("%d", &ncase);
    119     while (ncase--)
    120     {
    121         scanf("%d", &n);
    122         Node *root = new Node();
    123         for (int i = 0; i<n; ++i)
    124         {
    125             scanf("%s", str);
    126             insert(root, str);
    127         }
    128         getfail(root);
    129         scanf("%s", T);
    130         int ans = query(root, T);
    131         printf("%d
    ", ans);
    132     }
    133     return 0;
    134 }
    View Code
  • 相关阅读:
    flex 开发air 2.0 sdk 设置
    后来的我们
    岷县蓓蕾中学开展读书活动侧记
    c# 中文件操作 文件被锁住的问题
    wcf中配置文件
    ASP.NET中的session存储模式运用
    net中用户登录验证和基页面类
    jquery文件上传控件 Uploadify
    MongoDB基本介绍及一些用法
    用SQL删除重复记录的N种方法
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8492805.html
Copyright © 2011-2022 走看看