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.

现在才知道{scanf("%s",buf);ac.insert(buf);}和scanf("%s",buf),ac.insert(buf);不一样 TLE了十几次 一直在优化IO 以后要乖乖写分号了...

 #include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<queue>
 using namespace std;
 ;
 ;
 struct AC{
     int size,root,next[maxn][maxc],fail[maxn],end[maxn];
     int newNode(){
         //memset(next[size],-1,sizeof(next[size]));
         ;i<maxc;i++) next[size][i]=-;
         end[size]=;
         ++size;
         ;
     }
     int idx(char c){
         return c-'a';
     }
     void init(){
         size=;
         //memset(end,0,sizeof(end));
         root=newNode();
     }
     void build(){
         queue<int> Q;
         fail[root]=root;
         ;i<maxc;i++){
             ) next[root][i]=root;
             else fail[next[root][i]]=root,Q.push(next[root][i]);
         }
         while(!Q.empty()){
             int u=Q.front();Q.pop();
             ;i<maxc;i++){
                 ) next[u][i]=next[fail[u]][i];
                 else fail[next[u][i]]=next[fail[u]][i],Q.push(next[u][i]);
             }
         }
     }
     int query(char* s){
         ;
         int l=strlen(s);
         ;i<l;i++){
             n=next[n][s[i]-'a'];
             int trav=n;
             while(trav!=root){
                 count+=end[trav];
                 end[trav]=;
                 trav=fail[trav];
             }
         }
         return count;
     }
     void insert(char* s){
         int n=root;
         int l=strlen(s);
         ;i<l;i++){
             ) next[n][s[i]-'a']=newNode();
             n=next[n][s[i]-'a'];
         }
         end[n]++;
     }
 };
 AC ac;
 *maxn];
 int main()
 {
     int T;scanf("%d",&T);
     while(T--){
         int n;scanf("%d",&n);
         ac.init();
         ;i<n;i++) {
             scanf("%s",buf);ac.insert(buf);
         }
         ac.build();
         scanf("%s",buf);
         printf("%d\n",ac.query(buf));
     }
     ;
 }

HDOJ2222 Keywords Search-AC自动机的更多相关文章

  1. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  2. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  3. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  4. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

  6. Keywords Search AC自动机

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

  7. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  8. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  9. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  10. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

随机推荐

  1. 关于HTTP的几种

    301.404.200.304等HTTP状态,代表什么意思? 如果某项请求发送到您的服务器要求显示您网站上的某个网页(例如,用户通过浏览器访问您的网页或 Googlebot 抓取网页时),服务器将会返 ...

  2. 关于VS中文件属性的解释

    生成操作(BuildAction) 属性:BuildAction 属性指示 Visual Studio .NET 在执行生成时对文件执行的操作.BuildAction 可以具有以下几个值之一: 无(N ...

  3. 安装生物信息学软件-Biopython

    其实好多东西装过好多次,然而每次都要翻文档,经常掉进前面掉进过的坑...所以这里重新写一份指南,以防下次再装又忘了(魂淡我并不想再装了啊不要立flag) 1. 安装biopython 1.1 因为bi ...

  4. Ubuntu中添加eclipse

    环境:Ubuntu 14.04 步骤: 1.安装配置JDK,详见 http://my.oschina.net/u/1407116/blog/227084 2.下载eclipse 从官网http://w ...

  5. iOS 根据UIImage 修改UIImageView Frame (包括截取图片中间部分)

    iOS UIImageView 根据需求调整frame 1.图片的宽和高不相等,截取图片的中间部分,截取的部分Size明确 2.图片的宽度要等于其父视图的类的宽度,然后根据宽度计算高度,保证 图片不变 ...

  6. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

  7. Windowns的GVIM添加markdown语法支持

    gvim 7.4中其实也是有对markdown的语法文件,但格式支持并不全面,如行业代码``就没有实现. 修改方案: 从github下载plasticboy的markdown语法版本,windowns ...

  8. UILabel 的高度根据文字内容调整

    1.UILabel 对文字的自适应有两种方法. 1)将label的numberOfLines设为0;并添加自适应方法[titleLabel sizeToFit],但是这种方法并不理想. 2)根据文字的 ...

  9. yii使用MongoDB作为数据库服务软件[win7环境下](1)

    1.进入http://php.net,在站内搜索栏搜索mongodb,查看相关的安装步骤信息. 2.找到相应的php.ini配置文件,使用wampserver等服务器软件时,千万不要找错了php.in ...

  10. AndroidStudio 中的坑Error:(1, 0) Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRID

    将 build.gradle 中 的 classpath改为2.0.+ dependencies { classpath 'com.android.tools.build:gradle:2.0.+'然 ...