hdu2222 KeyWords Search AC自动机入门题
/**
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222
题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L(L <= 106)目标串,求目标串出现了多少个模式串。
思路:ac自动机入门题。。直接插入查询。
唯一需要特殊考虑的是存在多个相同的字符串;相同的字符串会在字典书上覆盖原先的。
解决方法1:用map<string,int>标记同一种字符串。之后利用标记来统计。
解决方法2:用num[i]标记字典树上某个节点为结尾的字符串出现次数。之后统计的时候,如果是第一次统计它,那么加上它,然后置为-1表示
下次不需要再统计它了。 AC自动机好文章:http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html
*/ ///解法1:
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *+;
const int sigma_size = ;
int cnt[];
map<string,int> mp;
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){return c-'a'; } void insert(char *s,int x)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = x;
} void find(char *T){
int n = strlen(T);
int j = ;
for(int i = ; i < n; i++){
int c = idx(T[i]);
//while(j&&!ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} void print(int j)
{
if(j){
cnt[val[j]] = ;
print(last[j]);
}
} void getFail(){
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u){f[u] = ; q.push(u); last[u] = ;}
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u){
ch[r][c] = ch[f[r]][c]; continue;
}//if(!u) continue;
q.push(u);
int v = f[r];
while(v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} } ac ;
char s[];
char t[][];
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
ac.clear();
mp.clear();
for(int i = ; i <= n; i++){
scanf("%s",t[i]);
ac.insert(t[i],i);
mp[string(t[i])] = i;///因为两个完全相同的字符串会覆盖原先的,所以用map标记属于同一个。这样可以都加到。
}
scanf("%s",s);
ac.getFail();
ms(cnt,);
ac.find(s);
int ans = ;
for(int i = ; i <= n; i++) ans += cnt[mp[string(t[i])]];
printf("%d\n",ans);
}
return ;
} /*
1
5
she
he
say
shr
her
yasherhs
*/ ///解法2:
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *+;
const int sigma_size = ;
int cnt[];
map<string,int> mp;
int num[maxnode];///统计在自动机上到达i节点的这个字符串的相同字符串的个数。
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){return c-'a'; } void insert(char *s,int x)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
num[sz] = ;
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = x;
num[u]++;
} void find(char *T){
int n = strlen(T);
int j = ;
for(int i = ; i < n; i++){
int c = idx(T[i]);
//while(j&&!ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} void print(int j)
{
if(j){
if(num[j]!=-){
cnt[val[j]] = num[j];
num[j] = -;
}
print(last[j]);
}
} void getFail(){
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u){f[u] = ; q.push(u); last[u] = ;}
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u){
ch[r][c] = ch[f[r]][c]; continue;
}//if(!u) continue;
q.push(u);
int v = f[r];
while(v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} } ac ;
char s[];
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
ac.clear();
mp.clear();
for(int i = ; i <= n; i++){
scanf("%s",s);
ac.insert(s,i);
}
scanf("%s",s);
ac.getFail();
ms(cnt,);
ac.find(s);
int ans = ;
for(int i = ; i <= n; i++) ans += cnt[i];
printf("%d\n",ans);
}
return ;
} /*
2
5
she
he
say
shr
her
yasherhs
2
ab
ab
aba
*/
hdu2222 KeyWords Search AC自动机入门题的更多相关文章
- HDU2222 Keywords Search ac自动机第一题
指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...
- HD2222 Keywords Search(AC自动机入门题)
然而还不是很懂=_= #include <iostream> #include <cstring> #include <algorithm> #include &l ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- 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自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
随机推荐
- 越狱iphone在cydia下插件后出现exit safe mode肿么办小教程
http://bbs.app111.com/thread-318898-1-1.html 从简单的开始..最简单的点击状态栏会弹出来一个窗口,那窗口有三个选择请选择第二个,然后等待它重启,重启后还没消 ...
- windows添加开机启动项
http://www.cnblogs.com/jokey/archive/2010/06/17/1759370.html添加开机启动项(通过注册表) 例子:增加QQ开机启动项 第一步:找到注册表的启动 ...
- Python学习笔记015——文件file的常规操作(二进制文件)
有时候诸如图片.音乐等信息不是以文本的形式存储的,而是二进制文件格式存储的. 有时候很多信息不是以文本文件的形式存储的 很多时候,都需要用到二进制读取文件.毕竟很多信息不是以文本的形式存储.例如:图片 ...
- DBA_实践指南系列5_Oracle Erp R12日常运维和管理(案例)
2013-12-05 Created By BaoXinjian
- 存储映射I/O
一个进程拥有独立并且连续虚拟地址空间,在32位体系结构中进程的地址空间是4G.不过,内核在管理进程的地址空间时是以内存区域为单位.内存区域是进程整个地址空间中一个独立的内存范围,它在内核中使用vm_a ...
- 支持向量机(SVM):应用实例
SVM 应用实例(人脸识别): from __future__ import print_function from time import time import logging import m ...
- Linux中telnet命令
telnet命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户 ...
- IP和java.net.InetAddress类的使用
一.IP 1.地址格式 互联网上每一台计算机都有一个唯一标示自己的标记,这个标记就是IP地址.IP 地址使用32 位长度二进制数据标示,一般在实际中看到的大部分IP地址都是以十进制的数据形式标示的,如 ...
- 在FPGA中使用for循环一定浪费资源吗?
渐渐地,发现自己已经习惯于发现细节,喜欢打破常规,真的非常喜欢这种feel. 相信很多人在书上或者博文上都有提出“在FPGA中使用for语句是很占用资源的”的观点,特权同学也不例外.那么,这种观点正确 ...
- Spring-两种配置容器
Spring提供了两种容器类型 SpringIOC容器是一个IOC Service Provider.提供了两种容器类型:BeanFactory和ApplicationContext.Sp ...