题目链接

统计一段字符串中有多少个模板串在里面出现过

#include<bits/stdc++.h>
using namespace std; const int N=; struct Trie
{
int next[N][];
int fail[N];// fail[i]表示i结点//
int end[N]; // end[i]表示以i结点为结尾的模式串个数
int L,root;
int newnode()
{
for(int i=;i<;i++)
next[L][i]=-;
end[L++]=;
return L-;
}
void init()
{
L=;
root=newnode();
}
void insert(char buf[])
{
int now=root;
for(int i=;buf[i];i++)
{
int ch=buf[i]-'a';
if(next[now][ch]==-)
next[now][ch]=newnode();
now=next[now][ch];
}
end[now]++;
}
void build()
{
queue<int> Q;
fail[root]=root;
for(int i=;i<;i++)
{
if(next[root][i]==-)
next[root][i]=root;
else
{
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
int now=Q.front();
Q.pop();
for(int i=;i<;i++)
{
if(next[now][i]==-)
next[now][i]=next[fail[now]][i];//
else
{
fail[next[now][i]]=next[fail[now]][i];//
Q.push(next[now][i]);
}
}
}
}
int query(char buf[])
{
int now=root;
int temp,ret=;
for(int i=;buf[i];i++)
{
int ch=buf[i]-'a';
now=next[now][ch];
temp=now;
while(temp!=root)
{
ret+=end[temp];
end[temp]=; //不作重复计数
temp=fail[temp];
}
}
return ret;
}
}ac; char buf[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
char str[];
int n;
ac.init();
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%s",str),ac.insert(str);
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
}

hdu_2222: Keywords Search(AC自动机模板题)的更多相关文章

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

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

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

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  3. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

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

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

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

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

  6. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

  7. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  8. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

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

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

  10. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

随机推荐

  1. cpp(第十三章)

    1.动态(晚期)联编需要显示定义复制构造函数,赋值运算符,虚构函数. 2.纯虚类不能声明对象. 3.赋值运算符的特征标随类而异. 4.返回类型协变,重新定义继承的方法,应确保与原来的原型完全相同,但如 ...

  2. Linux SSH安全技巧

    SSH服务器配置文件是/etc/ssh/sshd_conf.在你对它进行每一次改动后都需要重新启动SSH服务,以便让改动生效. 1.修改SSH监听端口默认情况下,SSH监听连接端口22,攻击者使用端口 ...

  3. angular指令ng-class巧用

    什么是ng-class ng-class最大的妙用就是可以根据你的逻辑表达式.来添加或移除对应的class ng-class是angular.js里面内置的一个指令. 项目中,有时候,我们需要根据需求 ...

  4. JAVAEE学习——struts2_03:OGNL表达式、OGNL与Struts2的结合和练习:客户列表

    一.OGNL表达式 1.简介 OGNL:对象视图导航语言.  ${user.addr.name} 这种写法就叫对象视图导航. OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能. 2.使用OG ...

  5. spring boot 入门操作(二)

    spring boot入门操作 使用FastJson解析json数据 pom dependencies里添加fastjson依赖 <dependency> <groupId>c ...

  6. Hibernate_Validator学习分享

    1.   Hibernate Validator介绍 1.1   背景 在任何时候,当你要处理一个应用程序的业务逻辑,数据校验是你必须要考虑和面对的事情.应用程序必须通过某种手段来确保输入进来的数据从 ...

  7. Java NIO学习笔记六 SocketChannel 和 ServerSocketChannel

    Java NIO SocketChannel Java NIO SocketChannel是连接到TCP网络socket(套接字)的通道.Java NIO相当于Java Networking的sock ...

  8. 解决laydate时间日期插件定位溢出

    laydate是一款比较好用的网页时间日期插件,不过用起来有一些细节问题需要我们手动去解决!例如:laydate兼容bootstrap 1. 默认情况 laydate弹出层默认对齐input左边框 2 ...

  9. while和do while循环的简要概述

    循环结构 1.while结构 在英文中"while"这个词的意思是"当",而在 Java 程序设计中,也可以将其理解为"当",其语法结构是: ...

  10. vue组件(Vue+webpack项目实战系列之三)

    组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.特别对于大型应用开发来说,尽量组件化,并且先造好轮子库,不要重复去写组件,这会显著提升项目 ...