HDU P2222 Keywords Search

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.

--by HDU;

http://acm.hdu.edu.cn/showproblem.php?pid=2222

《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《

  给一份Key_word和一串字符,查询有多少K_w在字串中;

  唉,你看这题,你看她,她又是一道萝莉裸题,你不知道,裸题不能抄题解吗?

  要自己打知道吗?

  。。。。

  本题显然是AC自动机,然而我却写成了TLE自动机(话说哪有数组开小就死循环的。。)

先建一棵trie

  代码:

    for(i=1;i<=n;i++){
        getchar();
        scanf("%s",key[i]);
        k=0;
        len=strlen(key[i])-1;
        for(j=0;j<=len;j++){
            if(!data[k].ch[key[i][j]-‘a‘])
                data[k].ch[key[i][j]-‘a‘]=++tot;
            k=data[k].ch[key[i][j]-‘a‘];
        }
        is_end[k]++;
    }

(有关trie的,POJ P2001)

  然后连上fail;

  bfs顺序求fail保证先求父节点i再求子节点j,fail[j]=fail[i].ch(子节点的fail为父节点的fail的子节点或父节点的fai的faill的子节点或...);

  代码:

void bfs_fail()
{
    memset(vis,0,sizeof(vis));
    int i,j;
    que[1]=0;h=0;t=1;
    while(h<t){
        ++h;
        for(i=0;i<=25;i++)
            if(data[que[h]].ch[i]){
                j=que[h];
                while(1){
                    if(data[j].ch[i]&&data[j].ch[i]!=data[que[h]].ch[i]){
                        fail[data[que[h]].ch[i]]=data[j].ch[i];   break;}
                    else{
                        if(!j)
                            break;
                        j=fail[j];
                    }
                }
                t++;
                if(!vis[data[que[h]].ch[i]]){
                    que[t]=data[que[h]].ch[i];
                    vis[que[t]]=1;
                }
            }
    }
}

这样自动机就建好了!!

然后是匹配:

代码:

//给我自动跑!!

请无视上行;

void match()
{
    int tem1=0,tem2=0,len;
    len=strlen(des);
    while(tem2!=len){
        if(data[tem1].ch[des[tem2]-‘a‘]){
            tem1=data[tem1].ch[des[tem2]-‘a‘];
            tem2++;
                find(tem1);
        }
        else{
            if(tem1==0)
                tem2++;
            tem1=fail[tem1];
        }
    }
}

你看到一个find(tem1)这是什么?

她的目的是在跑AC自动机时查询必须查询的值——查询未被查询的is_end标记,因为当我们查询到一个匹配的的点时,她的fail链上的is_end要被加入ans中,因为这些is_end所代表的key_word是你当匹配过的字符集(串?序列?)的后缀。

总代码如下:

  1 #include<cstdio>
  2 #include<cstring>
  3 using namespace std;
  4 int n,ans;
  5 char key[10001][101];
  6 struct ss{
  7     int ch[27];
  8 }data[500001];
  9 int tot;
 10 int is_end[500001];
 11 int fail[500001];
 12 int que[500000],h,t;
 13 char des[500001];
 14 int vis[500001];
 15 void bfs_fail();
 16 void work();
 17 void match();
 18 void find(int );
 19 int main()
 20 {
 21     int T;
 22     scanf("%d",&T);
 23     while(T--)
 24         work();
 25     return 0;
 26 }
 27 void work()
 28 {
 29     int i,j,k,len;
 30     memset(data,0,sizeof(data));
 31     memset(is_end,0,sizeof(is_end));
 32     memset(fail,0,sizeof(fail));
 33     scanf("%d",&n);
 34     for(i=1;i<=n;i++){
 35         getchar();
 36         scanf("%s",key[i]);
 37         k=0;
 38         len=strlen(key[i])-1;
 39         for(j=0;j<=len;j++){
 40             if(!data[k].ch[key[i][j]-‘a‘])
 41                 data[k].ch[key[i][j]-‘a‘]=++tot;
 42             k=data[k].ch[key[i][j]-‘a‘];
 43         }
 44         is_end[k]++;
 45     }
 46     bfs_fail();
 47     getchar();
 48     scanf("%s",des);
 49     ans=0;match();
 50     printf("%d\n",ans);
 51 }
 52 void bfs_fail()
 53 {
 54     memset(vis,0,sizeof(vis));
 55     int i,j;
 56     que[1]=0;h=0;t=1;
 57     while(h<t){
 58         ++h;
 59         for(i=0;i<=25;i++)
 60             if(data[que[h]].ch[i]){
 61                 j=que[h];
 62                 while(1){
 63                     if(data[j].ch[i]&&data[j].ch[i]!=data[que[h]].ch[i]){
 64                         fail[data[que[h]].ch[i]]=data[j].ch[i];   break;}
 65                     else{
 66                         if(!j)
 67                             break;
 68                         j=fail[j];
 69                     }
 70                 }
 71                 t++;
 72                 if(!vis[data[que[h]].ch[i]]){
 73                     que[t]=data[que[h]].ch[i];
 74                     vis[que[t]]=1;
 75                 }
 76             }
 77     }
 78 }
 79 void match()
 80 {
 81     int tem1=0,tem2=0,len;
 82     len=strlen(des);
 83     while(tem2!=len){
 84         if(data[tem1].ch[des[tem2]-‘a‘]){
 85             tem1=data[tem1].ch[des[tem2]-‘a‘];
 86             tem2++;
 87                 find(tem1);
 88         }
 89         else{
 90             if(tem1==0)
 91                 tem2++;
 92             tem1=fail[tem1];
 93         }
 94     }
 95 }
 96 void find(int tem)
 97 {
 98     int i;
 99     while(tem){
100         if(is_end[tem]){
101             ans+=is_end[tem];
102             is_end[tem]=0;
103         }
104         tem=fail[tem];
105     }
106 }

祝AC哟

时间: 2024-10-26 20:02:48

HDU P2222 Keywords Search的相关文章

HDU 2222 - Keywords Search

试个模板- - /* HDU 2222 - Keywords Search [ AC自动机 ] */ #include <bits/stdc++.h> using namespace std; const int N = 500005; const int SIZE = 26; struct Trie { int ch[N][SIZE]; int f[N], last[N], cnt[N], val[N]; int tot, ans; void init() { tot = 0; memset

HDU 2222——Keywords Search(AC自动机)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34020    Accepted Submission(s): 11009 Problem Description In the modern time, Search engine came into the life of everybody like

HDU 2222 Keywords Search AC自动机入门题

单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自动机的基础: 1 Trie, 以这个数据结构为基础的,不过增加一个fail指针和构造fail的函数 2 KMP,不是直接运用KMP,而是需要KMP的思想,KMP思想都没有的话,理解这个算法会更加吃力的. 注意本题的单词会有重复出现的,一个单词只能统计一次. 搜索了一下网上的题解,发现好多代码都是一大抄的啊,⊙﹏⊙b汗. 本博客的乃是原创代码,代码风格也是差不多固定的,转载请注明出处:http://blog.c

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

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67950    Accepted Submission(s): 22882 Problem Description In the modern time, Search engine came into the life of everybody lik

hdu 2222 Keywords Search(ac自动机入门题)

1 /************************************************************ 2 题目: Keywords Search(hdu 2222) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 4 算法: ac自动机 5 算法思想: 多个字符串匹配,也就是相当于多个kmp 6 ***********************************************************

HDU 2222 Keywords Search (AC自动机入门 模板)

AC自动机入门 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一.学习AC自动机之前得先有Trie树和KMP模式匹配算法的基础. AC自动机算法分为3步:1.构造一棵tire树  2.构造失败指针  3.进行模式匹配 AC自动机的优化:Trie图 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other

HDU 2222 Keywords Search AC自动机

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67122    Accepted Submission(s): 22584 Problem Description In the modern time, Search engine came into the life of everybody lik

HDU 2222 Keywords Search(瞎搞)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 50451    Accepted Submission(s): 16236 Problem Description In the modern time, Search engine came into the life of everybody lik

hdu 2222 Keywords Search(AC自动机入门)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 42138    Accepted Submission(s): 13289 Problem Description In the modern time, Search engine came into the life of everybody like