AC日记——Keywords Search hdu 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的更多相关文章
- Keywords Search HDU - 2222 ( ac自动机)模版题
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search HDU - 2222 AC自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- Keywords Search - HDU 2222(AC自动机模板)
题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串. 分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...
- Keywords Search HDU - 2222(ac自动机板题。。)
求一个字符串上有多少个匹配的单词 看着卿学姐的板子写的 指针形式: #include <iostream> #include <cstdio> #include <sst ...
- AC自动机---Keywords Search
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A Description In the moder ...
- AC日记——Number Sequence hdu 1711
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- AC日记——统计难题 hdu 1251
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- AC日记——病毒侵袭 hdu 2896
2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...
- AC日记——Paint Pearls hdu 5009
Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...
随机推荐
- [Java文件操作] 为文本文件添加行号
[思路]将文件中的内容按行读取存入一个字符串中,在输出时再为每一行加上行号. import java.io.*; public class Text { private String strFinal ...
- 多线程 定时器 Timer TimerTask
定时器是一种特殊的多线程,使用Timer来安排一次或者重复执行某个任务 package org.zln.thread; import java.util.Date; import java.util. ...
- group by 分组后 返回的是一个同属性的集合
group by 分组后 返回的是一个同属性的集合 我们可以遍历该集合
- Codeforces Round #401 (Div. 1) C(set+树状数组)
题意: 给出一个序列,给出一个k,要求给出一个划分方案,使得连续区间内不同的数不超过k个,问划分的最少区间个数,输出时将k=1~n的答案都输出 比赛的时候想的有点偏,然后写了个nlog^2n的做法,T ...
- MySQL 服务器安装及命令使用
本文来自实验楼相关部分的文档和实验操作过程. 一.MySQL简介 MySQL是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,提高了速度并提高了灵活性.My ...
- 【BZOJ 3123】 [Sdoi2013]森林 主席树启发式合并
我们直接按父子关系建主席树,然后记录倍增方便以后求LCA,同时用并查集维护根节点,而且还要记录根节点对应的size,用来对其启发式合并,然后每当我们合并的时候我们都要暴力拆小的一部分重复以上部分,总时 ...
- 如何写出规范的JavaScript代码
作为一名开发人员(WEB前端JavaScript开发),不规范的开发不仅使日后代码维护变的困难,同时也不利于团队的合作,通常还会带来代码安全以及执行效率上的问题.本人在开发工作中就曾与不按规范来开发的 ...
- 解决导出为Excel时文件名乱码的问题。
以前代码:public static void htmlToExcel(HttpContext context, string title, string html, string fileCss = ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B
B. Problems for Round time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 问题总结——window平台下grunt\bower安装后无法运行的问题
一.问题: 安装grunt或者bower后,在cmd控制台运行grunt -version 或者 bower -v会出现:“xxx不是内部或外部命令,也不是可运行的程序或批处理文件”,