zoukankan      html  css  js  c++  java
  • HDU2222 Keywords Search(AC自动机)

    Keywords Search

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


    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
     

    【思路】

           AC自动机模板的直接应用。

           需要注意的有:

        1)      要用val纪录该节点对应得单词数目,因为可能会有重复的串。

        2)      Val累计入cnt后清0,print可能会重复遍历一个相同的单词。

    【代码】

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #include<iostream>
     5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
     6 using namespace std;
     7 
     8 const int maxn = 10000+10;
     9 const int maxl = 50+5;
    10 const int maxnode = maxl*maxn;
    11 const int sigma = 26;
    12 
    13 struct ACautomaton {
    14     int ch[maxnode][sigma];
    15     int f[maxnode],val[maxnode],last[maxnode];
    16     int sz,cnt;
    17     
    18     void clear() {
    19         sz=1; cnt=0;
    20         memset(ch[0],0,sizeof(ch[0]));
    21         val[0]=f[0]=0;
    22     }
    23     int idx(char c) {  return c-'a';  }
    24     void insert(char* s) {
    25         int n=strlen(s),u=0;
    26         for(int i=0;i<n;i++) {
    27             int c=idx(s[i]);
    28             if(!ch[u][c]) {
    29                 memset(ch[sz],0,sizeof(ch[sz]));
    30                 val[sz]=0;
    31                 ch[u][c]=sz++;
    32             }
    33             u=ch[u][c];
    34         }
    35         val[u]++;
    36     }
    37     void print(int j) {
    38         if(j) {
    39             cnt+=val[j];
    40             val[j]=0;                //一定要置为0防止重复累计 
    41             print(last[j]);
    42         }
    43     }
    44     void find(char* s) {
    45         int n=strlen(s),j=0;
    46         for(int i=0;i<n;i++) {
    47             int c=idx(s[i]);
    48             while(j && !ch[j][c]) j=f[j];
    49             j=ch[j][c];
    50             if(val[j]) print(j);
    51             else if(last[j]) print(last[j]);
    52         }
    53     }
    54     void get_Fail() {
    55         queue<int> q;
    56         f[0]=0;
    57         for(int c=0;c<sigma;c++) {
    58             int u=ch[0][c];
    59             if(u)  {  f[u]=last[u]=0; q.push(u);  }
    60         }
    61         while(!q.empty()) {
    62             int r=q.front(); q.pop();
    63             for(int c=0;c<sigma;c++) {
    64                 int u=ch[r][c];
    65                 if(!u) continue;
    66                 q.push(u);
    67                 int v=f[r];
    68                 while(v && !ch[v][c]) v=f[v];
    69                 f[u]=ch[v][c];
    70                 last[u]=val[f[u]]?f[u]:last[f[u]];
    71             }
    72         }
    73     }
    74 }ac;
    75 
    76 int n;
    77 
    78 int main() {
    79     //freopen("in.in","r",stdin);
    80     //freopen("out_me.out","w",stdout);
    81     int T;
    82     scanf("%d",&T);
    83     while(T--) {
    84         char s[maxl];
    85         scanf("%d",&n);
    86         ac.clear();
    87         FOR(i,1,n) {
    88             scanf("%s",s);
    89             ac.insert(s);
    90         }
    91         ac.get_Fail();
    92         char T[1000010];
    93         scanf("%s",T);
    94         ac.find(T);
    95         printf("%d
    ",ac.cnt);
    96     }
    97     return 0;
    98 }

          

  • 相关阅读:
    爬虫
    vue进级
    el-upload加header
    上传图片到服务器报错 {errMsg: "uploadFile:fail ssl hand shake error"}
    【Node.js】论一个低配版Web实时通信库是如何实现的1( WebSocket篇)
    【Node.js】 bodyparser实现原理解析
    【JavaScript】EventEmitter的前端实现
    【JavaScript】当我们尝试用JavaScipt测网速
    git命令
    【Java】利用注解和反射实现一个"低配版"的依赖注入
  • 原文地址:https://www.cnblogs.com/lidaxin/p/4992042.html
Copyright © 2011-2022 走看看