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. chromium 一些设置 --插件安装

    一.安装flash插件 打开网页 http://get.adobe.com/cn/flashplayer/otherversions/ 选择如图 所示  fp 18 for Opera and Chr ...

  2. zip函数

    zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个包含元组的列表. x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zip(x, y, z) ...

  3. 更新包地址安装新版node.js

    # apt-get update # apt-get install -y python-software-properties software-properties-common # add-ap ...

  4. 黑马程序员:Java编程_动态代理

    =========== ASP.Net+Android+IOS开发..Net培训.期待与您交流!=========== 长沙人从长沙的代理商手中买宏基电脑和直接跑到宏基总部买电脑,最终的主体业务目标有 ...

  5. CentOS 7 为firewalld添加开放端口及相关资料

    1.运行.停止.禁用firewalld 启动:# systemctl start  firewalld 查看状态:# systemctl status firewalld 或者 firewall-cm ...

  6. systemtap

    http://www.ibm.com/developerworks/library/l-systemtap/index.html http://wiki.eclipse.org/Linux_Tools ...

  7. NET-SNMP配置

    配置/etc/snmp/snmpd.conf such as below : ============================================== com2sec notCon ...

  8. 我终于搞清楚为什么谷歌地图获取到的联通3G基站与大家手头的基站表不同了

    我终于搞清楚这个问题了,大家使用谷歌地图手机版.MobileTrack以及网优用的FieldTest获取到的WCDMA基站Cellid为什么不是大家手头的CellTrack91或基站表里的数字了... ...

  9. yII中利用urlManager将URL改写成restful风格 这里主要涉及url显示样式

    1.打开config文件夹下面的mian.php   2.修改内容   如把地址http://www.test.com/index.php?r=site/page/sid/1修改为http://www ...

  10. javascript中的闭包解析

    学习javaScript已经有一段时间了,在这段时间里,已经感受到了JavaScript的种种魅力,这是一门神奇的语言,同时也是一门正在逐步完善的语言,相信在大家的逐步修改中,这门语言会逐步的完善下去 ...