zoukankan      html  css  js  c++  java
  • HDU 2222 Keywords Search(查询关键字)

    HDU 2222 Keywords Search(查询关键字)

    Time Limit: 2000/1000 MS (Java/Others)

    Memory Limit: 131072/131072 K (Java/Others)

    【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.

    当今社会,诸如谷歌,百度等搜索引擎不断深入人心。

    Wiskey想把这个功能加入他的图片搜索系统。

    每张图都有一串很长的描述,当用户输入一些关键字去搜索图片时,系统会匹配描述图片的关键字并显示有最多关键字匹配的图片。

    【Input】

    【输入】

    First line will contain one integer means how many cases will follow by.

    Each case will contain one 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.

    第一行有一个整数随后测试用例数量。

    每个测试用例。

    每个测试用例有一个整数N表示关键字数量与随后有N个关键字。(N <= 10000)

    每个关键字只包含字符'a'-'z',并且其长度不大于50。

    最后一行是此图片的描述,并且其长度不大于1000000。

    【Output】

    【输出】

    Print how many keywords are contained in the description.

    输出描述中包含的关键字数量。

    【Sample Input - 输入样例】

    【Sample Output - 输出样例】

    1

    5

    she

    he

    say

    shr

    her

    yasherhs

    3

    【题解】

        AC自动机入门题,不过数据不全面,里面没有考虑回头匹配子串情况的数据(个人觉得这是KMP的AC自动机的主要不同之处,Trie图不会,哪天被打脸了再说)

    例如:

    1

    2

    abc

    b

    abc

    输出1的代码居然也能A……而且貌似这个问题被提出来很久了,一直没有改善。

    注意匹配的是关键字数量,关键字可以相同,不过可以看成匹配一个关键字就去掉一个关键字。

    【代码 C++】

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 struct Node{
     5     int next[26], fail, isWord;
     6 }tr[500005];
     7 int it;
     8 char text[1000005];
     9 
    10 void build(){
    11     it = 0;
    12     memset(tr, 0, sizeof(tr));
    13     int n, i, j, w;
    14     char word[55];
    15     scanf("%d", &n), getchar();
    16     while (n--){
    17         gets(word);
    18         for (i = j = 0; word[i]; ++i){
    19             w = word[i] - 'a';
    20             if (!tr[j].next[w]) tr[j].next[w] = ++it;
    21             j = tr[j].next[w];
    22         }
    23         ++tr[j].isWord;
    24     }
    25 }
    26 void setFail(){
    27     std::queue<int> q, last;
    28     int now, pre, next, i, j;
    29     for (i = 0; i < 26; ++i){
    30         if (tr[0].next[i]) q.push(tr[0].next[i]), last.push(0);
    31     }
    32 
    33     while (!q.empty()){
    34         now = q.front(); q.pop();
    35         pre = last.front(); last.pop();
    36         for (i = 0; i < 26; ++i){
    37             next = tr[now].next[i];
    38             if (next){
    39                 q.push(next);
    40                 if (tr[pre].next[i]) j = pre;
    41                 else j = 0;
    42                 if (tr[j].next[i]){
    43                     tr[next].fail = tr[j].next[i];
    44                     last.push(tr[j].next[i]);
    45                 }
    46                 else last.push(0);
    47             }
    48         }
    49     }
    50 }
    51 int fid(){
    52     gets(text);
    53     int i, iTR, opt, w, temp;
    54     for (i = iTR = opt = 0; text[i]; ++i){
    55         w = text[i] - 'a';
    56         if (tr[iTR].next[w]) iTR = tr[iTR].next[w];
    57         else{
    58             if (iTR) --i;
    59             iTR = tr[iTR].fail;
    60         }
    61         for (temp = iTR; ~tr[temp].isWord; temp = tr[temp].fail){
    62             opt += tr[temp].isWord;
    63             tr[temp].isWord = -1;
    64         }
    65     }
    66     return opt;
    67 }
    68 int main(){
    69     int t;
    70     scanf("%d", &t);
    71     while (t--){
    72         build();
    73         setFail();
    74         printf("%d
    ", fid());
    75     }
    76     return 0;
    77 }
    自己面向图片编程的代码……
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 struct Node{
     5     int next[26], fail, isWord;
     6 }tr[500005];
     7 int it;
     8 
     9 void build(){
    10     it = 0;
    11     memset(tr, 0, sizeof(tr));
    12     int n, i, j, w;
    13     char word[55];
    14     scanf("%d", &n), getchar();
    15     while (n--){
    16         gets(word);
    17         for (i = j = 0; word[i]; ++i){
    18             w = word[i] - 'a';
    19             if (!tr[j].next[w]) tr[j].next[w] = ++it;
    20             j = tr[j].next[w];
    21         }
    22         ++tr[j].isWord;
    23     }
    24 }
    25 void setFail(){
    26     std::queue<int> q;
    27     int now, next, i;
    28     for (i = 0; i < 26; ++i) if (tr[0].next[i]) q.push(tr[0].next[i]);
    29 
    30     while (!q.empty()){
    31         now = q.front(); q.pop();
    32         for (i = 0; i < 26; ++i){
    33             if (next = tr[now].next[i]){
    34                 q.push(next);
    35                 tr[next].fail = tr[tr[now].fail].next[i];
    36             }
    37             else tr[now].next[i] = tr[tr[now].fail].next[i];
    38         }
    39     }
    40 }
    41 int fid(){
    42     char text[1000005];
    43     gets(text);
    44     int i, iTR, opt, temp;
    45     for (i = iTR = opt = 0; text[i]; ++i){
    46         iTR = tr[iTR].next[text[i] - 'a'];
    47         for (temp = iTR; ~tr[temp].isWord; temp = tr[temp].fail){
    48             opt += tr[temp].isWord;
    49             tr[temp].isWord = -1;
    50         }
    51     }
    52     return opt;
    53 }
    54 int main(){
    55     int t;
    56     scanf("%d", &t);
    57     while (t--){
    58         build();
    59         setFail();
    60         printf("%d
    ", fid());
    61     }
    62     return 0;
    63 }
    参考kuangbin巨后稍微简化的代码


     

  • 相关阅读:
    ②.kubernetes service
    c2p
    ⑤.docker dockerfile
    ④.docker volume
    ②.docker image
    ③.docker container
    ①.docker介绍
    三剑客之grep
    ⑦.shell 数组
    shell 正则
  • 原文地址:https://www.cnblogs.com/Simon-X/p/5679993.html
Copyright © 2011-2022 走看看