题目大意:判断模式串中是否出现模板。

代码如下:

# include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; const int N=1000000; int cnt;
int ch[N+5][26];
int f[N+5],val[N+5]; void init()
{
cnt=0;
memset(ch,0,sizeof(ch));
memset(val,0,sizeof(val));
} int idx(char x)
{
return x-'a';
} void insert(char *p)
{
int n=strlen(p);
int root=0;
for(int i=0;i<n;++i){
int c=idx(p[i]);
if(!ch[root][c]) ch[root][c]=++cnt;
root=ch[root][c];
}
val[root]=1;
} void getFail()
{
queue<int>q;
f[0]=0;
for(int i=0;i<26;++i) if(ch[0][i]){
f[ch[0][i]]=0;
q.push(ch[0][i]);
}
while(!q.empty()){
int u=q.front();
q.pop();
if(val[f[u]]) val[u]=1;
for(int i=0;i<26;++i){
if(ch[u][i]){
q.push(ch[u][i]);
f[ch[u][i]]=ch[f[u]][i];
}else{
ch[u][i]=ch[f[u]][i];
}
}
}
} char p[N+5]; bool match(char *p)
{
int n=strlen(p);
int u=0;
for(int i=0;i<n;++i){
int c=idx(p[i]);
u=ch[u][c];
if(val[u]) return true;
}
return false;
} int main()
{
int n;
while(~scanf("%d",&n))
{
init();
for(int i=0;i<n;++i){
scanf("%s",p);
insert(p);
}
getFail();
scanf("%s",p);
if(match(p)) printf("YES\n");
else printf("NO\n");
}
return 0;
}

  

hihoCoder-1036 (AC自动机模板题)的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  3. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  4. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  5. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

  6. HDu-2896 病毒侵袭,AC自动机模板题!

    病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...

  7. [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]

    AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

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

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

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

    题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...

随机推荐

  1. Oracle数据备份bat

    @echo on setlocal rem 设置数据库和备份文件参数... set sid=testorcl set username=testname set password=testpwd se ...

  2. postgis数据库文件shapefile导入 dbf file (.dbf) can not be opened.shapefile import failed.

    Destination: public.train_polylineSource File: C:\Documents and Settings\Administrator\桌面\ffffff\tra ...

  3. PHP的autoload机制的实现解析

    在使用PHP的OO模式开发系统时,通常大家习惯上将每个类的实现都存放在一个单独的文件里,这样会很容易实现对类进行复用,同时将来维护时也很便利 一.autoload机制概述 在使用PHP的OO模式开发系 ...

  4. struts(三) ---OGNL的学习和理解

    OGNL:Object graphic Navgation Language(对象图形的导航语言)

  5. DB2表分区删除

    近日,由于部门数据库读库空间过小,提出删除掉两个月之前日志表的分区(数据库分区是按时间月分区),记述如下: 上网搜索资料发现删除表分区大概分这么几步: 1.查询需要删除掉的分区: select t.D ...

  6. [分享] RT7LITE精简后无法封装解决办法

    eagleonly 发表于 2016-6-9 11:00:01  https://www.itsk.com/forum.php?mod=viewthread&tid=368090&hi ...

  7. js根据当前时间获取当前季度,月份,和第几周

    function jidu()  {   var getMonthWeek = function (a, b, c) {    var date = new Date(a, parseInt(b) - ...

  8. 51nod 1412 AVL树的种类(dp)

    题目链接:51nod 1412 AVL树的种类 开始做的时候把深度开得过小了结果一直WA,是我天真了.. #include<cstdio> #include<cstring> ...

  9. Java 概述

    一 Java 程序的种类 1)Java 小应用程序(Java Applet) — 在Web浏览器中运行(内嵌Java虚拟机) —特定标记 <APPLET CODE="HelloWorl ...

  10. Unix Shell 程序设计 —— 正则表达式

    参考:http://www.cnblogs.com/erichhuang/archive/2012/03/13/2394119.html 简介: 简单的说,正则表达式是一种可以用于模式匹配和替换的强有 ...