题目大概是给一个主串,询问若干个模式串出现次数,其中有些模式串要求不能重叠。

对于可以重叠的就是一个直白的多模式匹配问题;而不可重叠,在匹配过程中贪心地记录当前匹配的主串位置,然后每当出现一个新匹配根据记录的位置判断这个新匹配是否成立,最后更新位置。

另外,考虑到数据可以出现多个模式串相同的情况,实现要做一些处理:

 #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自动机)的更多相关文章

  1. zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)

    /** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...

  2. ZOJ3228 Searching the String —— AC自动机 + 可重叠/不可重叠

    题目链接:https://vjudge.net/problem/ZOJ-3228 Searching the String Time Limit: 7 Seconds      Memory Limi ...

  3. ZOJ3228 - Searching the String(AC自动机)

    题目大意 给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠 题解 第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE...后来想 ...

  4. ZOJ 3228 Searching the String(AC自动机)

    Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to d ...

  5. ZOJ3228 Searching the String (AC自动机)

    Searching the String Time Limit: 7 Seconds                                      Memory Limit: 129872 ...

  6. 【AC自动机】zoj3228 Searching the String

    对所有模式串建立AC自动机. 每个单词结点要记录该单词长度. 然后在跑匹配的时候,对每个单词结点再处理3个值,代表可重叠的匹配次数,不可重叠的匹配次数,以及“上一次不可重叠的匹配位置”,这样结合单词长 ...

  7. 【XSY3320】string AC自动机 哈希 点分治

    题目大意 给一棵树,每条边上有一个字符,求有多少对 \((x,y)(x<y)\),满足 \(x\) 到 \(y\) 路径上的边上的字符按顺序组成的字符串为回文串. \(1\leq n\leq 5 ...

  8. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  9. HDU 6096 String (AC自动机)

    题意:给出n个字符串和q个询问,每次询问给出两个串 p 和 s .要求统计所有字符串中前缀为 p 且后缀为 s (不可重叠)的字符串的数量. 析:真是觉得没有思路啊,看了官方题解,真是好复杂. 假设原 ...

  10. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

随机推荐

  1. Spring+Quartz实现定时任务的配置方法

    1.Scheduler的配置 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" ...

  2. Java笔记--泛型总结与详解

    泛型简介: 在泛型没有出来之前,编写存储对象的数据结构是很不方便的.如果要针对每类型的对象写一个数据结构,     则当需要将其应用到其他对象上时,还需要重写这个数据结构.如果使用了Object类型, ...

  3. Python机器学习库scikit-learn实践

    原文:http://blog.csdn.net/zouxy09/article/details/48903179 一.概述 机器学习算法在近几年大数据点燃的热火熏陶下已经变得被人所“熟知”,就算不懂得 ...

  4. 二叉树建立,先序、中序、后序遍历(c实现)

    建立树ABC##DE#G##F###,输出 #include <stdio.h> #include <stdlib.h> #define ElemType char //节点声 ...

  5. poj1573 模拟

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11270   Accepted: 5487 Des ...

  6. django 1.5+ 权限设计浅析

    权限关系图 依赖app: django.contrib.auth django.contrib.contenttype admin后台的权限控制解析 (path/to/django.contrib.a ...

  7. QEMU 使用的镜像文件:qcow2 与 raw

    qcow2 的基本原理 qcow2 镜像格式是 QEMU 模拟器支持的一种磁盘镜像.它也是可以用一个文件的形式来表示一块固定大小的块设备磁盘.与普通的 raw 格式的镜像相比,有以下特性: 更小的空间 ...

  8. 解决ecshop登陆自动退出的莫名现象

    最近在做ecshop的二次开发,程序发布后测试出现一个莫名的问题.点击几次页面后出现session丢失,需要重复登陆:本地怎么测试也都无法重现问题.一开始以为是修改程序的问题,可是怎么找都找不着问题所 ...

  9. Java for LeetCode 079 Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  10. PHP--TP框架----生成验证码的方式

    TP框架----生成验证码的方式 xianshi.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...