【ZOJ 3228】Searching the String 【AC自动机】
题意
给出n个模式串和一个文本串,输出各个模式串在文本串中出现的次数。模式串有两种类型,0类型代表可以有重叠,1类型代表不能有重叠。模式串可能出现重复。
分析
算是AC自动机的模板题?
因为模式串可以重复,所以如果直接插入并且用val数组来保存模式串的编号的话,后面出现的会把前面出现的给覆盖。所以我这里用了一个map来保存每个模式串在trie中的编号。
如何处理1类型不能有重叠的情况?对于1类型的每个模式串,记录一下它的长度和上次匹配到的位置。当再次匹配到这个模式串的时候,看一下这次的位置和上次位置的差有没有大于它的长度,如果大于,则说明这个可以选择不会重叠。
我们在插入模式串的时候不区分是哪种类型,在进行find的时候也不进行区分,找到一个模式串以后,同时更新两种类型。只在最后输出的时候区分一下。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map> using namespace std;
const int maxnode=;
const int sigma_size=;
const int maxs=+;
char T[maxs],P[maxs][],type[maxs];
map<string,int>ms;
int kase;
struct AC_Automata{
int ch[maxnode][sigma_size],val[maxnode],cnt[maxnode][],tim[maxnode];
int f[maxnode],last[maxnode],len[maxnode];
int sz;
void init(){
sz=;
memset(ch[],,sizeof(ch[]));
memset(cnt,,sizeof(cnt));
memset(tim,,sizeof(tim));
val[]=;
ms.clear();
}
void insert(char *s){
int n=strlen(s),u=;
for(int i=;i<n;i++){
int c=s[i]-'a';
if(!ch[u][c]){
ch[u][c]=sz;
memset(ch[sz],,sizeof(ch[sz]));
val[sz++]=;
}
u=ch[u][c];
}
val[u]=;
len[u]=n;
string S=(string)s;
ms[S]=u;
}
void getFail(){
queue<int>q;
f[]=last[]=;
for(int i=;i<sigma_size;i++){
int u=ch[][i];
if(u){
q.push(u);
f[u]=last[u]=;
}
}
while(!q.empty()){
int r=q.front();q.pop();
for(int i=;i<sigma_size;i++){
int u=ch[r][i];
if(!u)continue;
q.push(u);
int v=f[r];
while(v&&!ch[v][i])v=f[v];
f[u]=ch[v][i];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void print(int i,int pos){
if(val[i]){
cnt[i][]++;
if(tim[i]+len[i]<=pos){
cnt[i][]++;
tim[i]=pos;
}
print(last[i],pos);
}
}
void find(char *s){
int n=strlen(s),j=;
for(int i=;i<n;i++){
int c=s[i]-'a';
while(j&&!ch[j][c])j=f[j];
j=ch[j][c];
if(val[j])
print(j,i+);
else if(last[j])
print(last[j],i+);
}
}
}ac;
int n;
int main(){
kase=;
while(scanf("%s",T)!=EOF){
++kase;
scanf("%d",&n);
ac.init();
for(int i=;i<=n;i++){
scanf("%d %s",&type[i],P[i]);
ac.insert(P[i]);
}
ac.getFail();
ac.find(T);
printf("Case %d\n",kase);
for(int i=;i<=n;i++){
string S=(string)P[i];
int u=ms[S];
printf("%d\n",ac.cnt[u][type[i]]);
// printf("%d\n",u);
}
printf("\n");
}
return ;
}
【ZOJ 3228】Searching the String 【AC自动机】的更多相关文章
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- ZOJ - 3228 Searching the String (AC自己主动机)
Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...
- ZOJ 3228 Searching the String (AC自己主动机)
题目链接:Searching the String 解析:给一个长串.给n个不同种类的短串.问分别在能重叠下或者不能重叠下短串在长串中出现的次数. 能重叠的已经是最简单的AC自己主动机模板题了. 不能 ...
- ZOJ3228 Searching the String —— AC自动机 + 可重叠/不可重叠
题目链接:https://vjudge.net/problem/ZOJ-3228 Searching the String Time Limit: 7 Seconds Memory Limi ...
- zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)
/** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...
- ZOJ3228 - Searching the String(AC自动机)
题目大意 给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠 题解 第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE...后来想 ...
- zoj 3228:Searching the String
Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...
- ZOJ - 3430 Detect the Virus —— AC自动机、解码
题目链接:https://vjudge.net/problem/ZOJ-3430 Detect the Virus Time Limit: 2 Seconds Memory Limit: 6 ...
- 【XSY3320】string AC自动机 哈希 点分治
题目大意 给一棵树,每条边上有一个字符,求有多少对 \((x,y)(x<y)\),满足 \(x\) 到 \(y\) 路径上的边上的字符按顺序组成的字符串为回文串. \(1\leq n\leq 5 ...
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
随机推荐
- __getitem__ 专用方法
(1) __getitem__ 专用方法很简单.像普通的方法 clear,keys 和 values 一样,它只是重定向到字典,返回字典的值.但是怎么调用它呢?哦,你可以直接调用 __getitem ...
- nginx grpc 试用
1. 编译 wget https://nginx.org/download/nginx-1.13.10.tar.gz tar xvf nginx-1.13.10.tar.gz cd nginx-1.1 ...
- 一款直接时空处理分析的开源数据库---geomesa
一款直接时空处理分析的开源数据库---geomesa,可用于交通轨迹数据存储分析等相关领域, 在分布式列数据库的基础上进行扩展,目前支持Accumulo, HBase, Cassandra, and ...
- Discuz! X2.5RC 全新安装图文教程
http://www.discuz.net/thread-2660015-1-1.html 一步步教你利用Discuz X2.5建设论坛视频教程(174集) http://down.51cto.com ...
- 笔记:webpack 打包参数 mode development
webpack 打包参数 mode development 在开发时使用 webpack 打包后不压缩,所以只需要在 webpack 打包命令中加上 --mode mode development 即 ...
- CF 1093E Intersection of Permutations——CDQ分治
题目:http://codeforces.com/contest/1093/problem/E 只能想到转化成查询一个区间里值在一个范围里的数的个数…… 没有想到这样适合用主席树套树状数组维护.不过据 ...
- RK3288 指令查看LCD分辨率
通过下面指令可以查看当前系统设置的分辨率. root@xxx:/ # cd sys/class/graphics/fb0 cd sys/class/graphics/fb0 root@xxx:/sys ...
- 微信卡券开发,代金券修改卡券信息返回40145错误码: invalid update! Can not both set PayCell and CenterCellInfo(include: center_title, center_sub_title, center_url). hint: [DZ9rna0637ent1]
修改代金券,接口返回的数组是这样的内容 Array ( [errcode] => 40145 [errmsg] => invalid update! Can not both set ...
- 怎样用java生成GUID与UUID
GUID是一个128位长的数字,一般用16进制表示.算法的核心思想是结合机器的网卡.当地时间.一个随机数来生成GUID.从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义 ...
- 阻塞队列之六:LinkedBlockingDeque
一.LinkedBlockingDeque简介 java6增加了两种容器类型,Deque和BlockingDeque,它们分别对Queue和BlockingQueue进行了扩展. Deque是一个双端 ...