ZOJ3228 Searching the String(AC自动机)
题目大概是给一个主串,询问若干个模式串出现次数,其中有些模式串要求不能重叠。
对于可以重叠的就是一个直白的多模式匹配问题;而不可重叠,在匹配过程中贪心地记录当前匹配的主串位置,然后每当出现一个新匹配根据记录的位置判断这个新匹配是否成立,最后更新位置。
另外,考虑到数据可以出现多个模式串相同的情况,实现要做一些处理:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 666666
//-1 none, 0 overlap, 1 not, 2 both
int tn,ch[MAXN][],fail[MAXN],flag[MAXN],len[MAXN];
int insert(char *s,int k){
int x=,l=;
for(int i=; s[i]; ++i){
int y=s[i]-'a';
if(ch[x][y]==) ch[x][y]=++tn;
x=ch[x][y];
++l;
}
len[x]=l;
if(flag[x]==-) flag[x]=k;
else if(flag[x]== && k==) flag[x]=;
else if(flag[x]== && k==) flag[x]=;
return x;
}
void getfail(){
memset(fail,,sizeof(flag));
queue<int> que;
for(int i=; i<; ++i){
if(ch[][i]) que.push(ch[][i]);
}
while(!que.empty()){
int x=que.front(); que.pop();
for(int i=; i<; ++i){
if(ch[x][i]){
que.push(ch[x][i]);
fail[ch[x][i]]=ch[fail[x]][i];
}else ch[x][i]=ch[fail[x]][i];
}
}
}
int last[MAXN],ans[][MAXN],first[],second[];
char str[];
void query(){
int x=;
for(int i=; str[i]; ++i){
int y=str[i]-'a';
int tmp=x=ch[x][y];
while(tmp){
if(flag[tmp]!=- && flag[tmp]!=) ++ans[][tmp];
if(flag[tmp]!=- && flag[tmp]!=){
if(last[tmp]==- || i-last[tmp]>=len[tmp]){
last[tmp]=i;
++ans[][tmp];
}
}
tmp=fail[tmp];
}
}
}
int main(){
int n,cse=;
char s[];
while(~scanf("%s",str)){
tn=;
memset(ch,,sizeof(ch));
memset(flag,-,sizeof(flag));
scanf("%d",&n);
for(int i=; i<n; ++i){
scanf("%d%s",&first[i],s);
second[i]=insert(s,first[i]);
}
getfail();
memset(ans,,sizeof(ans));
memset(last,-,sizeof(last));
query();
printf("Case %d\n",++cse);
for(int i=; i<n; ++i){
printf("%d\n",ans[first[i]][second[i]]);
}
putchar('\n');
}
return ;
}
ZOJ3228 Searching the String(AC自动机)的更多相关文章
- zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)
/** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...
- 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自动机)
题目大意 给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠 题解 第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE...后来想 ...
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- ZOJ3228 Searching the String (AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 ...
- 【AC自动机】zoj3228 Searching the String
对所有模式串建立AC自动机. 每个单词结点要记录该单词长度. 然后在跑匹配的时候,对每个单词结点再处理3个值,代表可重叠的匹配次数,不可重叠的匹配次数,以及“上一次不可重叠的匹配位置”,这样结合单词长 ...
- 【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 ...
- HDU 6096 String (AC自动机)
题意:给出n个字符串和q个询问,每次询问给出两个串 p 和 s .要求统计所有字符串中前缀为 p 且后缀为 s (不可重叠)的字符串的数量. 析:真是觉得没有思路啊,看了官方题解,真是好复杂. 假设原 ...
- 2017多校第6场 HDU 6096 String AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
随机推荐
- PHP无限极分类实现
简单版的PHP生成无限极分类代码.其中包括了数据库设计.以及输出分类HTML代码. SQL代码 CREATE TABLE `district` ( `id` int(10) unsigned NOT ...
- android menu showAsAction属性
app中有一个菜单(menu),showAsAction主要是针对这个菜单的显示起作用的,它有三个可选项:always:总是显示在界面上 never:不显示在界面上,只让出现在右边的三个点中 ifRo ...
- luarocks install with lua5.1 and luajit to install lapis
# in luarocks source directory...git clone https://github.com/archoncap/luarockscd luarocks ./config ...
- 如何在Linux上实现文件系统的自动检查和修复?
Linux文件系统有可能在各种各样的情况下受到损坏,比如系统崩溃.突然断电.磁盘断开,或者文件节点 (i-node)不小心被覆盖等等,因此需要定期检查文件系统,而说到检查和修复Linux文件系统,fs ...
- raw格式镜像文件压缩并转换为qcow2格式
raw格式文件,这个比较占用空间,你可以用以下命令将其压缩并转换成qcow2格式. # virt-sparsify --compress --convert qcow2 ubuntu.img ubun ...
- 完整java开发中JDBC连接数据库代码和步骤 JDBC连接数据库
JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.l ...
- iOS7中都Bar的透明问题
/* New behavior on iOS 7. Default is YES. You may force an opaque background by setting the property ...
- php 301
2013年7月1日 13:35:45 PHP在301跳转的时候,如果是跨域跳转,记着把要跳转到的URL添上"http://"
- codeforces 486B.OR in Matrix 解题报告
题目链接:http://codeforces.com/problemset/problem/486/B 题目意思:给出一个m行n列的矩阵B(每个元素只由0/1组成),问是否可以利用矩阵B,通过一定的运 ...
- 实现dom元素拖动
本文主要写一下如何实现dom元素拖动,目前使用jquery库实现之. 主要的注释附在代码中,大家可以根据代码画一个小的窗口模型图,以便于理解. <!DOCTYPE html> <ht ...