2222

思路:

  ac自动机模板题;

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 500005 struct TreeNodeType {
int count; TreeNodeType *fail; TreeNodeType *next[]; TreeNodeType()
{
fail=NULL,count=;
for(int i=;i<;i++) next[i]=NULL;
}
};
struct TreeNodeType *root,*que[maxn]; int n; char str[maxn*],word[]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void insert(char *ch)
{
int temp,len=strlen(ch);
TreeNodeType *p=root;
for(int i=;i<len;i++)
{
temp=ch[i]-'a';
if(p->next[temp]==NULL) p->next[temp]=new TreeNodeType;
p=p->next[temp];
}
p->count++;
} void build()
{
int tail=,head=;que[head]=root;
while(head<tail)
{
TreeNodeType *p=que[head++],*temp=NULL;
for(int i=;i<;i++)
{
if(p->next[i]==NULL) continue;
if(p==root) p->next[i]->fail=root;
else
{
temp=p->fail;
while(temp!=NULL)
{
if(temp->next[i]!=NULL)
{
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(temp==NULL) p->next[i]->fail=root;
}
que[tail++]=p->next[i];
}
}
} int query()
{
int pos,len=strlen(str),res=;TreeNodeType *p=root;
for(int i=;i<len;i++)
{
pos=str[i]-'a';
while(p->next[pos]==NULL&&p!=root) p=p->fail;
if(p->next[pos]!=NULL) p=p->next[pos];else p=root;
TreeNodeType *temp=p;
while(temp!=root&&temp->count!=-) res+=temp->count,temp->count=-,temp=temp->fail;
}
return res;
} int main()
{
int T;
in(T);
while(T--)
{
root=new TreeNodeType;
in(n);for(int i=;i<=n;i++) gets(word),insert(word);
build(),scanf("%s",str),printf("%d\n",query());
}
return ;
}

AC日记——Keywords Search hdu 2222的更多相关文章

  1. Keywords Search HDU - 2222 ( ac自动机)模版题

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

  2. Keywords Search HDU - 2222 AC自动机板子题

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

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

    题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串.   分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...

  4. Keywords Search HDU - 2222(ac自动机板题。。)

    求一个字符串上有多少个匹配的单词 看着卿学姐的板子写的 指针形式: #include <iostream> #include <cstdio> #include <sst ...

  5. AC自动机---Keywords Search

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A Description In the moder ...

  6. AC日记——Number Sequence hdu 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. AC日记——统计难题 hdu 1251

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. AC日记——病毒侵袭 hdu 2896

    2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...

  9. AC日记——Paint Pearls hdu 5009

    Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...

随机推荐

  1. 解决Mysql错误Too many connections的方法

    MySQL数据库 Too many connections出现这种错误明显就是 mysql_connect 之后忘记 mysql_close:当大量的connect之后,就会出现Too many co ...

  2. require.js 模块化

    什么是模块化? 将若干功能进行封装,以备将来被重复使用. 为什么要前端模块化? 将公共功能进行封装实现复用 灵活解决依赖 解决全局变量污染 如何实现前端模块化? <!DOCTYPE html&g ...

  3. 一道前端面试题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify();

    偶然在群里看到了这道题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify(); 这道题主要是对JavaScript对象原型的考察.

  4. BZOJ2819 Nim 【dfn序 + lca + 博弈论】

    题目 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输.这个游戏是有必胜策略的. ...

  5. CENSORING

    CENSORING 题目描述 FJ为它的奶牛订阅了很多杂志,balabala.......,其中有一些奶牛不宜的东西(比如如何煮牛排). FJ将杂志中所有的文章提取出来组成一个长度最多为10^5的字符 ...

  6. 【CF edu 30 D. Merge Sort】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. net.sf.json与fastjson两种jar包的使用

    首先说清楚:这两种方式是进行json解析的两种不同的方式而已,哪一种都可以. 一.引入net.sf.json包 首先用net.sf.json包,当然你要导入很多包来支持commons-beanutil ...

  8. 解读python小练习

    1.新建一个函数,判断是不是int 类型,并测试,不是抛出错误def adder(x, y):"""Return x + y if they are both integ ...

  9. barba 页面渲染

    a.css html, body { padding:; margin: 0 } ol.menu { width: 100%; text-align: left; padding: 0 !import ...

  10. JavaScript中cookie使用

    转自:http://www.cnblogs.com/yjzhu/archive/2012/11/26/2789032.html 一.什么是 cookie? cookie 就是页面用来保存信息,比如自动 ...