【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 ...
随机推荐
- bzoj 3158 千钧一发——网络流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3158 发现偶数之间一定满足第二个条件:奇数之间一定满足第一个条件 ( \( (2m+1)^{ ...
- Spring Boot 入门之缓存和 NoSQL 篇(四)
原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...
- 15.Python实现识别登录验证码(入门)
1.若想识别登录验证码,需要安装:Tesseract-OCR,其下载地址为:http://jaist.dl.sourceforge.net/project/tesseract-ocr-alt/tess ...
- 亚马逊MWS开发套路演示
MWS是商城网络服务的缩写,具体介绍看这里http://docs.developer.amazonservices.com/zh_CN/dev_guide/DG_IfNew.html.MWS就是一组A ...
- 关于如何利用Pocket CHM Pro制作帮助文档
关于如何利用Pocket CHM Pro制作帮助文档 编写人:CC阿爸 2015-4-6 今天在这里,我想与大家一起分享如何利用Pocket CHM Pro制作软件系统的帮助文档,在此做个小结,以供参 ...
- cowboy的路由方式
直接贴代码 route_helper.erl -module(route_helper). -export([get_routes/]). get_routes() -> [ {'_', [ % ...
- snmpd 子代理模式编译测试
1.参考链接 1)Net-snmp添加子代理示例 https://blog.csdn.net/eyf0917/article/details/39546651 2.操作步骤 1)网络拷贝下面的文件 ...
- java代码----------实现写出循环
总结:对于循环次数累加.等等总是不知道在哪一行插入 package com.mmm; //输出10个数字中,小于5的数字 public class hai { public static void m ...
- 【语音识别】Microsoft Speech Platform 自学笔记2 环境要求与安装过程
笔记人:又吹风 时 间:2012/12/16 主要内容:Microsoft Speech Platform的环境要求与安装过程. 上次也说过了,当前Microsoft Speech Platform最 ...
- Android:不同drawable文件夹的区别
4.0后,新建android工程,会自动生成drawable,drawalbe-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi,drawable-xxh ...