HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串。
思路:用int型的end数组记录出现,AC自动机即可。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<string>
- #include<algorithm>
- #include<queue>
- #define maxn 500005
- int T,n;
- char s[];
- struct Trie{
- int root,L;
- int next[maxn][],fail[maxn],end[maxn];
- int newnode(){
- for (int i=;i<;i++)
- next[L][i]=-;
- end[L++]=;
- return L-;
- }
- void clear(){
- L=;root=newnode();
- }
- void insert(char s[]){
- int now=root,len=strlen(s);
- for (int i=;i<len;i++){
- if (next[now][s[i]-'a']==-) next[now][s[i]-'a']=newnode();
- now=next[now][s[i]-'a'];
- }
- end[now]++;
- }
- void build(){
- std::queue<int>Q;
- int now=root;
- for (int i=;i<;i++)
- if (next[now][i]==-) next[now][i]=root;
- else
- fail[next[now][i]]=root,Q.push(next[now][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 s[]){
- int now=root,len=strlen(s),res=;
- for (int i=;i<len;i++){
- now=next[now][s[i]-'a'];
- int tmp=now;
- while (tmp!=root){
- res+=end[tmp];
- end[tmp]=;
- tmp=fail[tmp];
- }
- }
- return res;
- }
- }ac;
- int main(){
- scanf("%d",&T);
- while (T--){
- scanf("%d",&n);
- ac.clear();
- for (int i=;i<n;i++){
- scanf("%s",s);
- ac.insert(s);
- }
- ac.build();
- scanf("%s",s);
- printf("%d\n",ac.query(s));
- }
- }
HDU 2222 Keywords Search (AC自动机)的更多相关文章
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- hdoj 2222 Keywords Search(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
随机推荐
- Codeforces 566F Clique in the Divisibility Graph
http://codeforces.com/problemset/problem/566/F 题目大意: 有n个点,点上有值a[i], 任意两点(i, j)有无向边相连当且仅当 (a[i] mod a ...
- Altium Designer规则
1.PCB规则 是PCB设计中至关重要的一个环节:保证PCB符合电气要求.机械加工(精度)要求:为自动布局.布线和部分手动布局.布线操作提供依据 为规则检查提供依据,PCB编辑期间,AD会实时地进行一 ...
- 一些pyhon的学习资料
一直没有时间学习python,都说python语法简洁优美.但是我看它的语法还是不爽啊,没C/C++好阅读.但是C/C++又必须了解底层的计算机模型.着实会门槛高一些.特别是C++,对于我来说简直就是 ...
- cv:显示Linux命令运行进度
cv: 显示 cp.mv 等命令的进度 2014-07-14 By toy Posted in Apps Edit on GitHub 在 Linux 系统中 , 大多数命令从来都是信奉 “ 沉默是金 ...
- 第26讲 对话框AlertDialog的自定义实现
第26讲对话框AlertDialog的自定义实现 比如我们在开发过长当中,要通过介绍系统发送的一个广播弹出一个dialog.但是dialog必需是基于activity才能呈现出来,如果没有activi ...
- (转)Android中截取当前屏幕图片
该篇文章是说明在Android手机或平板电脑中如何实现截取当前屏幕的功能,并把截取的屏幕保存到SDCard中的某个目录文件夹下面.实现的代码如下: /** * 获取和保存当前屏幕的截图 */ priv ...
- Java小项目--坦克大战(version1.0)
Java小项目--坦克大战<TankWar1.0> 这个小项目主要是练习j2se的基础内容和面向对象的思想.项目实现了基本的简单功能,我方一辆坦克,用上下左右键控制移动方向,按F键为发射炮 ...
- Multithreading: How to Use the Synchronization Classes
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源) 翻译文章来源: msdn - Multithreading: How to Use t ...
- gnuplot常用技巧
一. 基础篇: 在linux命令提示符下运行gnuplot命令启动,输入quit或q或exit退出. 1.plot命令 gnuplot> plot sin(x) with l ...
- [置顶] ./build_native 时出现please define NDK_ROOT
在一次帮朋友弄cygwin交叉编译时出现了这个问题 cygwin是按照成功了,make-v,以及gcc-v都没出现问题,就是在./build_native 时出现please define NDK_R ...