D - Searching the String (AC自动机)
题目链接:https://cn.vjudge.net/contest/281961#problem/D
题目大意:给你一个模式串,然后给你多个匹配串,匹配串的类型是包括可以覆盖的以及不可覆盖的。
具体思路:对于可以覆盖的字符串,我们就按照以前的方法来就行了,对于不可以覆盖的字符串,我们通过两个数组,一个是last和pos数组,对于不可覆盖的, 当前字符位置 - last[当前节点] <= pos[当前节点]。last数组记录的是当前这个字符串上次出现的位置。
AC代码:
#include<iostream>
#include<stack>
#include<stdio.h>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
# define ll long long
const int maxn = 2e5+;
const int maxm = 1e6+;
char str1[maxn],str2[maxn];
int tot,ch[maxn][];
int type[maxn],pos[maxn],node[maxn],val[maxn];
int fail[maxn],last[maxn],ans[maxn][];
void add(int t)
{
int p=;
int len=strlen(str2);
for(int i=; i<len; i++)
{
int u=str2[i]-'a';
if(!ch[p][u])
ch[p][u]=++tot;
p=ch[p][u];
pos[p]=i+;
}
node[t]=p;
val[p]=;
}
void getfail()
{
queue<int>q;
for(int i=; i<; i++)
{
if(ch[][i])
q.push(ch[][i]);
}
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=; i<; i++)
{
int u=ch[top][i];
if(u==)
continue;
q.push(u);
int v=fail[top];
while(v&&ch[v][i]==)
v=fail[v];
fail[u]=ch[v][i];
// last[u]=val[fail[u]] ? fail[u] : last[fail[u]];
}
}
}
void cal(int t,int i)
{
while(t)
{
ans[t][]++;
// cout<<i<<" "<<last[t]<<" "<<pos[t]<<endl;
if(i-last[t]>=pos[t])ans[t][]++,last[t]=i;
t=fail[t];
// break;
}
}
void getans()
{
memset(last,-,sizeof(last));
int len=strlen(str1);
int p=;
for(int i=; i<len; i++)
{
int u=str1[i]-'a';
while(p&&ch[p][u]==)
p=fail[p];
p=ch[p][u];
if(val[p])
cal(p,i);
else if(fail[p])
cal(fail[p],i);
}
}
void init(){
tot=;
memset(ch,,sizeof(ch));
memset(val,,sizeof(val));
memset(fail,,sizeof(fail));
memset(last,,sizeof(last));
memset(ans,,sizeof(ans));
}
int main()
{
// memset(last,-1,sizeof(last));
int n;
int Case=;
while(~scanf("%s",str1)){
init();
scanf("%d",&n);
for(int i=; i<n; i++){
scanf("%d %s",&type[i],str2);
add(i);
}
getfail();
getans();
printf("Case %d\n",++Case);
for(int i=; i<n; i++){
printf("%d\n",ans[node[i]][type[i]]);
}
printf("\n");
}
return ;
}
D - 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 ...
- 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...后来想 ...
- 【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 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
- NOIP模拟 string - ac自动机
题目大意: 给n个字符串(100位以内),和一个长串s(100000位以内),求n个字符串在s中出现的次数.然后给出m次修改,每次修改s中的一个字符,对于每次修改,输出更新后的答案(修改会保存). 题 ...
- 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 ...
随机推荐
- 美国运营商推送假5G图标:用户当场蒙圈了
面对5G大潮,大家都想“争当第一”.美国运营商AT&T想出奇招,打算玩一把“障眼法”. 据外媒报道,AT&T的用户从明年开始会在手机右上角看到“5G E”的图标.当然,这并不是他们的手 ...
- 【 Gym 101116K 】Mixing Bowls(dfs)
BUPT2017 wintertraining(15) #4H Gym - 101116K 题意 给定一个菜谱,大写的单词代表混合物,小写的代表基础原料.每个混合物由其它混合物或基础原料组成,不会间接 ...
- 投入机器学习的怀抱?先学Python吧
前两天写了篇文章,给想进程序员这个行当的同学们一点建议,没想到反响这么好,关注和阅读数都上了新高度,有点人生巅峰的感觉呀.今天趁热打铁,聊聊我最喜欢的编程语言——Python. 为什么要说Python ...
- Android Service服务的生命周期
与activity类似,服务也存在生命周期回调方法,你可以实现这些方法来监控服务的状态变化,并在适当的时机执行一些操作. 以下代码提纲展示了服务的每个生命周期回调方法: public class Ex ...
- Shell基础知识(五)
shell中同样有数组的概念,获取数组中的元素要使用下标[],并且下标的值必须大于等于0.数据的各项特性见下例: #!/bin/bash array1=(1 2 3 999) echo ${array ...
- 面试 -- fragment生命周期
Android 3.0 (Api 11)引入: Fragment具有重用,易适配(平板和手机之间的)优点: 依赖Activity,生命周期受到Activity的生命周期影响: fragment生命周期 ...
- 【codevs1048】石子归并(初级版)
采用动态规划的原因:合并有一定次序,即:只能相邻石子进行合并. 阶段:当前合并了的区间长度 状态:区间的左右端点 状态转移方程:\(dp[l][r]=min\{dp[l][r],dp[l][k]+dp ...
- zookeeper安装和使用(Windows环境)
zookeeper安装和使用(Windows环境) 2017年11月27日 10:36:07 董昊炘的博客 阅读数:14785 标签: zookeeperwindows zookeeper 一.简 ...
- gdb调试1--包括反汇编及文件编码设置
一.编译 gcc的完整编译过程大致为:预处理->编译->汇编->链接 前三个步骤分别对应了-E.-S.-c三个选项. 编译阶段 命令 截断后的产物 C源程序 预处理 gcc ...
- springboot-24-restTemplate的使用
项目中使用的是HttpClient, 后来改成springboot, 偶然间发现restTemplate { "Author": "tomcat and jerry&qu ...