zoukankan      html  css  js  c++  java
  • HDU-2222文字检索

    题目:
    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. 

    InputFirst 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. 
    OutputPrint how many keywords are contained in the description.Sample Input

    1
    5
    she
    he
    say
    shr
    her
    yasherhs

    Sample Output

    3
    题意就是说给你一些模式串,问你有哪几个在目标串中出现过,(⊙o⊙)…,和洛谷那道末班体一模一样,就是多了一个多组数据。
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cmath>
     4 #include<iostream>
     5 #include<cstring>
     6 #include<queue>
     7 using namespace std;
     8 
     9 int n,cnt=1;
    10 char s[1000007];
    11 struct Node
    12 {
    13     int c[26],suf,flag;
    14     void init()
    15     {
    16         suf=flag=0;
    17         memset(c,0,sizeof(c));    
    18     }    
    19 }tree[1000007];
    20 
    21 void init()
    22 {
    23     for (int i=0;i<=cnt;i++)
    24         tree[i].init();
    25     cnt=1;    
    26 }
    27 void Ins()
    28 {
    29     int head=1,l=strlen(s);
    30     for (int i=0;i<l;i++)
    31     {
    32         int now=s[i]-'a';
    33         if (!tree[head].c[now]) tree[head].c[now]=++cnt;
    34         head=tree[head].c[now];
    35     }
    36     tree[head].flag++;
    37 }
    38 void Make_AC()
    39 {
    40     for (int i=0;i<26;i++) tree[0].c[i]=1;
    41     int head=0,tail=1;
    42     queue<int>q;
    43     while (!q.empty()) q.pop();
    44     tree[1].suf=0;
    45     q.push(1);
    46     while (!q.empty())
    47     {
    48         int u=q.front();
    49         q.pop();
    50         for (int i=0;i<26;i++)
    51             if (tree[u].c[i])
    52             {
    53                 tree[tree[u].c[i]].suf=tree[tree[u].suf].c[i];
    54                 q.push(tree[u].c[i]);
    55             }
    56             else tree[u].c[i]=tree[tree[u].suf].c[i];
    57     }
    58 }
    59 void Solve()
    60 {
    61     scanf("%s",s);
    62     int ans=0,head=1,len=strlen(s);
    63     for (int i=0;i<len;i++)
    64     {
    65         int now=s[i]-'a';
    66         head=tree[head].c[now];
    67         for (int j=head;j&&tree[j].flag!=-1;j=tree[j].suf)
    68             ans+=tree[j].flag,tree[j].flag=-1;
    69     }
    70     printf("%d
    ",ans);
    71 }
    72 int main()
    73 {    
    74     int Cas;
    75     scanf("%d",&Cas);
    76     while (Cas--)
    77     {
    78         init();
    79         scanf("%d",&n);
    80         while (n--){scanf("%s",s);Ins();}
    81         Make_AC();
    82         Solve();
    83     }
    84 }
     
  • 相关阅读:
    Tomcat启动提示At least one JAR was scanned for TLDs yet contained no TLDs
    elasticsearch 相关操作
    ES下载与安装
    分词器
    Go语言中import导入包时:点. 、下划线_ 、别名的用法
    删除某库中所有表
    yum 安装mysql
    chromedriver下载及配置
    前端库在Core Web项目中的引入和使用
    The database provider attempted to register an implementation of the 'IRelationalTypeMappingSource' service.
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7241826.html
Copyright © 2011-2022 走看看