【hdu2222-Keywords Search】AC自动机基础裸题
http://acm.hust.edu.cn/vjudge/problem/16403
题意:给定n个单词,一个字符串,问字符串中出现了多少个单词。(若单词her,he,字符串aher中出现了两个单词)
题解:
每个单词末尾节点sum=1;
find的时候每个点都顺着fail往上跳,加上该节点的sum,然后将这个sum清了;
注意同一个单词出现多次只算一次。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std; const int N=,L=;
char s[L];
int num,n;
struct node{
int son[];
int fail,cnt;
}a[N*];
queue<int> q; void clear(int x)
{
a[x].cnt=;
a[x].fail=;
memset(a[x].son,,sizeof(a[x].son));
} void trie(char *c)
{
int l=strlen(c);
int x=;
for(int i=;i<l;i++)
{
int t=c[i]-'a'+;
if(!a[x].son[t])
{
num++;
clear(num);
a[x].son[t]=num;
}
x=a[x].son[t];
}
a[x].cnt++;
} void buildAC()
{
while(!q.empty()) q.pop();
for(int i=;i<=;i++)
if(a[].son[i]) q.push(a[].son[i]);
while(!q.empty())
{
int x=q.front();q.pop();
int fail=a[x].fail;
for(int i=;i<=;i++)
{
int y=a[x].son[i];
if(y)
{
a[y].fail=a[fail].son[i];
q.push(y);
}
else a[x].son[i]=a[fail].son[i];
}
}
} int find(char *c)
{
int l=strlen(c);
int x=,ans=;
for(int i=;i<l;i++)
{
int t=c[i]-'a'+;
while(x && !a[x].son[t]) x=a[x].fail;
x=a[x].son[t];
int p=x;
while(p && a[p].cnt!=-)
{
ans+=a[p].cnt;
a[p].cnt=-;
p=a[p].fail;
}
}
return ans;
} int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
num=;
clear();
for(int i=;i<=n;i++)
{
scanf("%s",s);
trie(s);
}
buildAC();
scanf("%s",s);
printf("%d\n",find(s));
}
return ;
}
【hdu2222-Keywords Search】AC自动机基础裸题的更多相关文章
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search ac自动机第一题
指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
随机推荐
- Mac系统下安装Homebrew后无法使用brew命令,-bash: brew: command not found
使用如下命令: sudo vim .bash_profile 然后输入以下代码: export PATH=/usr/local/bin:$PATH 再使用以下命令使配置生效: source .bash ...
- Hbase表格设计
Rowkey设计 Region: 基于RowKey的分区,可理解成MySQL的水平切分. 每个Region Server就是Hadoop集群中一台机器上的一个进程. 比如我们的有1-300号的RowK ...
- PRO*C 函数事例 2 -- 数据库操作
Pro*C Oracle 的嵌入式开发,数据库处理部分最好能提取到一个模块,按照对不同数据库表的操作分成不同的.pc文件(如 DbsInstStat.pc).将此模块编译成库(c文件编译时链接此库), ...
- 「个人训练」Can you solve this equation?(HDU-2199)
题意与分析 纯粹水题.本来想做下放松心情的,结果还是被坑了qaq 重点就是在浮点误差.比较左右的下次就直接上1e-10,别看着题目说1e-4然后给个-5,结果暴wa.气傻了..... 代码 #incl ...
- 《python核心编程第二版》第8章习题
8–1. 条件语句. 请看下边的代码 # statement Aif x > 0:# statement Bpasselif x < 0:# statement Cpasselse:# s ...
- Cassandra 数据库设计
Cassandra 2.* CQL3.1 最近更新:2015-10-30 索引的设计 在Cassandra中经常会发现,索引不够用,不好用,各种不强大. 比如,我关注的人的需求uid + follow ...
- linux备忘录-正则表达式与文件格式化处理
正则表达式 POSIX标准的符号 [:alnum:] -> 英文大小写字母和数字 0-9,A-Z,a-z [:alpha:] -> 英文大小写字母 A-Z,a-z [:blank:] -& ...
- 关于Android Studio启动后自己的配置
根据Android Stduio自己设置的配置,我们在执行一些操作时可能不向教程那样,此时就要看教程上的Android Stduio的设置.
- IDEA使用maven构建时控制台中文乱码的解决办法
使用maven clean install 项目时控制台中文乱码,解决办法如下: Setting->maven->runner VMoptions: -Dfile.encoding=UTF ...
- Activiti工作流(一)——Activiti Diagram
工作流解决在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某个预期的业务目标,或者促使此目标的实现. 使用Eclipse开发,需要安排工作流插件,详情见下面. Name ...