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. 黑马程序员——OC语言 核心语法 (3)

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)分类Category 1) 基本用途 如何在不改变原来类模型的前提下 ...

  2. Python学习笔记-字典

    字典是python中唯一内建的映射类型. 创建字典phonebook = {'Alice':'2341','Beth':'9102'} 可以使用dict通过其他映射或者键值对的序列建立字典.关键值参数 ...

  3. 网站后台调用winform MessageLoopApartment

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. hive的Query和Insert,Group by,Aggregations(聚合)操作

    1.Query (1)分区查询 在查询的过程中,采用那个分区来查询是通过系统自动的决定,但是必须是在分区列上基于where子查询. SELECT page_views.* FROM page_view ...

  5. nslog一些用法

    1.nslog打印方法出来 NSLog(@"%@",NSStringFromSelector(_cmd)); 2.debug模式下打印一些信息,release模式下则不打印 #if ...

  6. CSS 选择器【详解】

    转自:http://www.cnblogs.com/polk6/archive/2013/07/19/3142142.html CSS 选择器及各样式引用方式介绍 一个好的界面,是一个Web吸引人们最 ...

  7. 系统不支持curl

    解决安装pinphp时出现的“系统不支持curl!”   今天在本机安装phppin开源程序时,提示“系统不支持curl!”错误. 由于我本机是UBUNTU系统,所以直接通过apt-get进行安装. ...

  8. Ambari是什么?

    Ambari目标 解决Hadoop生态系统部署 部署:hadoop组件间有依赖,包括配置.版本.启动顺序.权限配置等. 部署过程跟踪.能够展示出部署过程中每个步骤的状态及相关信息. 多机部署问题,当集 ...

  9. linux 下find命令 --查找文件名

    1.在某目录下查找名为"elm.cc"的文件 find /home/lijiajia/ -name elm.cc 2.查找文件名中包含某字符(如"elm")的文 ...

  10. 《Android深度探索》(卷1)HAL与驱动开发读后感

    第1章:安卓系统移植与驱动开发概述 这一章主要概括的介绍了安卓驱动开发和系统移植的主要内容,对安卓与Linux驱动做了一个总体的介绍.通过对第一章的学习,使我对Linux驱动开发有了一个感性的认识.在 ...