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 }
     
  • 相关阅读:
    JS—图片压缩上传(单张)
    vue 使用jssdk分享
    微信JS-SDK选择图片遇到的坑
    手把手教你实现一个微信自动回复机器人
    SSH实现远程控制
    使用Apache服务部署静态网站
    Rhel7安装及网卡、yum、vmtools配置和修改主机名
    基础工具之消息队列、线程池、缓冲区抽象、事件循环和日志实现
    I/O多路复用方案
    Java按字节截取字符串(GBK编码、UTF-8编码实现)
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7241826.html
Copyright © 2011-2022 走看看