• hdu:2222:Keywords Search(AC自动机模板题)


    Keywords Search

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


    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
     
     

    题意

    给定n个单词,一个字符串,问字符串中出现了多少个单词。(若单词her,he,字符串aher中出现了两个单词)

    code

    2018-07-23

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 
     5 const int N = 500100;
     6 char str[N << 1], s[110];
     7 int ch[N][26], fail[N], val[N], last[N], q[N], L, R, Index, Ans;
     8 
     9 void clear() {
    10     Index = 0, Ans = 0;
    11     memset(ch, 0, sizeof(ch));
    12     memset(val, 0, sizeof(val));
    13     memset(last, 0, sizeof(last));
    14     memset(fail, 0, sizeof(fail));
    15 }
    16 void Insert(char *s) {
    17     int len = strlen(s), u = 0;
    18     for (int i=0; i<len; ++i) {
    19         int c = s[i] - 'a';
    20         if (!ch[u][c]) ch[u][c] = ++Index;
    21         u = ch[u][c];
    22     }
    23     val[u] ++;
    24 }
    25 void build() {
    26     L = 1;R = 0;
    27     fail[0] = 0;
    28     for (int c=0; c<26; ++c) {
    29         int u = ch[0][c];
    30         if (u) fail[u] = 0, q[++R] = u, last[u] = 0;
    31     }
    32     while (L <= R) {
    33         int u = q[L++];
    34         for (int c=0; c<26; ++c) {
    35             int v = ch[u][c];
    36             if (!v) {
    37                 ch[u][c] = ch[fail[u]][c];
    38                 continue;
    39             }
    40             q[++R] = v;
    41             int p = fail[u];
    42             while (p && !ch[p][c]) p = fail[p];
    43             fail[v] = ch[p][c];
    44             last[v] = val[fail[v]] ? fail[v] : last[fail[v]];
    45         }
    46     }
    47 }
    48 void solve(int j) {
    49     if (!j) return ;
    50     if (val[j]) {
    51         Ans += val[j];
    52         val[j] = 0;
    53     }
    54     solve(last[j]);
    55 }
    56 void find(char *s) {
    57     int j = 0, len = strlen(s);
    58     for (int i=0; i<len; ++i) {
    59         int c = s[i] - 'a';
    60         j = ch[j][c];
    61         if (val[j]) solve(j);
    62         else if (last[j]) solve(last[j]);
    63     }
    64 }
    65 
    66 int main () {
    67     int T,n;scanf("%d",&T);
    68     while (T--) {
    69         clear();
    70         scanf("%d", &n);
    71         while (n--) {
    72             scanf("%s", s);
    73             Insert(s);
    74         }
    75         build();
    76         scanf("%s",str);
    77         find(str);
    78         printf("%d
    ",Ans);
    79     }
    80     return 0;
    81 }
    View Code

    2017-08-16

      1 #include<cstdio>
      2 #include<algorithm>
      3 #include<cstring>
      4 #include<queue>
      5 
      6 using namespace std;
      7 
      8 const int MAXN = 500100;
      9 
     10 char str[1000100],s[110];
     11 
     12 struct Aczdj{
     13     int ch[MAXN][26],val[MAXN],last[MAXN],fail[MAXN],size,ret;
     14     void clear()
     15     { 
     16         memset(ch[0],0,sizeof(ch[0]));
     17         memset(val,0,sizeof(val));
     18         memset(fail,0,sizeof(fail));
     19         size = 0;
     20         ret = 0;
     21     }
     22     int idx(char c)
     23     {
     24         return c-'a';
     25     }
     26     void insert(char *s)
     27     {
     28         int u = 0,len = strlen(s);
     29         for (int i=0; i<len; ++i)
     30         {
     31             int c = idx(s[i]);
     32             if (!ch[u][c])
     33             {
     34                 ch[u][c] = ++size;
     35                 val[size] = 0;
     36                 memset(ch[size],0,sizeof(ch[size]));
     37             }
     38             u = ch[u][c];
     39         }
     40         val[u] ++;
     41     }
     42     void getfail()
     43     {
     44         queue<int>q;
     45         fail[0] = 0;
     46         for (int c=0; c<26; ++c) 
     47         {
     48             int u = ch[0][c];
     49             if (u) 
     50             {
     51                 fail[u] = 0;q.push(u);last[u] = 0;
     52             }
     53         }
     54         while (!q.empty())
     55         {
     56             int r = q.front();q.pop();
     57             for (int c=0; c<26; ++c)
     58             {
     59                 int u = ch[r][c];
     60                 if (!u)
     61                 {
     62                     ch[r][c] = ch[fail[r]][c];//有这一行就可以减去89行 
     63                     continue;
     64                 }
     65                 q.push(u);
     66                 int v = fail[r];
     67                 while (v && !ch[v][c]) v = fail[v];
     68                 fail[u] = ch[v][c];
     69                 last[u] = val[fail[u]] ? fail[u] : last[fail[u]];
     70             }
     71         }
     72     }
     73     void solve(int j)
     74     {
     75         if (!j) return ;
     76         if (val[j])
     77         {
     78             ret += val[j];
     79             val[j] = 0;
     80         }
     81         solve(last[j]);
     82     }
     83     void find(char *T)
     84     {
     85         int j = 0,len = strlen(T);
     86         for (int i=0; i<len; ++i)
     87         {
     88             int c = idx(T[i]);
     89         //    while (j && !ch[j][c]) j = fail[j];
     90             j = ch[j][c];
     91             if (val[j]) solve(j);
     92             else if (last[j]) solve(last[j]);
     93         }
     94     }
     95     
     96 }ac;
     97 
     98 int main()
     99 {
    100     int t,n;
    101     scanf("%d",&t);
    102     while (t--) 
    103     {
    104         ac.clear();
    105         scanf("%d",&n);
    106         while (n--)
    107         {
    108             scanf("%s",s);
    109             ac.insert(s);
    110         }
    111         ac.getfail();
    112         scanf("%s",str);
    113         ac.find(str);
    114         printf("%d
    ",ac.ret);
    115     }
    116     return 0;
    117 }
    View Code
  • 相关阅读:
    The Network Adapter could not establish the connection问题研究
    条件更新或插入
    如何解决Oracle临时表空间过大
    oracle的临时表空间写满磁盘空间解决改问题的步骤
    如何在oracle中缩小临时表空间?ORA-01652无法在表空间中扩展temp
    oracle mysql sql 根据一张表更新另一张表
    Don’t Repeat Yourself,Repeat Yourself
    mysql oracle sql获取树 父级 子级 及自己
    hutool BigExcelWriter 下的autoSizeColumnAll异常问题
    Java手写数组栈
  • 原文地址:https://www.cnblogs.com/mjtcn/p/7371870.html
Copyright © 2020-2023  润新知