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 ...
随机推荐
- 浅谈教你如何掌握Linux系统
linux能做什么?相信绝大数人都有这样的疑问.可以玩吃鸡吗?可以玩lol吗? 如果你是以娱乐的名义来评价linux的可用性,对不起,linux可能不适合你,因为linux是一个工具,他是教你聪明的, ...
- Linux环境变量PATH
查看PATH:echo $PATH以添加mongodb server为列修改方法一:export PATH=/usr/local/mongodb/bin:$PATH//配置完后可以通过echo $PA ...
- BZOJ3522[Poi2014]Hotel——树形DP
题目描述 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽 ...
- jQuery文档处理总结
<!DOCTYPE html> <html lang="cn"> <head> <meta charset="UTF-8&quo ...
- Codeforces976E Well played! 【贪心】
题目分析: 由于乘二的收获很大,所以我们可以证明乘的数一定是同一个,接着排序后依次选取,判断一下即可. 题目代码: #include<bits/stdc++.h> using namesp ...
- 最大获利 HYSBZ - 1497 (最大权闭合图)
最大权闭合图: 有向图,每个点有点权,点权可正可负.对于任意一条有向边i和j,选择了点i就必须选择点j,你需要选择一些点使得得到权值最大. 解决方法: 网络流 对于任意点i,如果i权值为正,s向i连容 ...
- Leapin' Lizards HDU - 2732 (恶心的建图。。)
这道题其实不难...就是建图恶心了点....emm... 题意: 多源多汇 + 拆边 青蛙跳柱子, 每根柱子都有一定的承载能力, 青蛙跳上去之后柱子的承载能力就会减一,跳到边界就能活 跳不到就over ...
- MT【228】整数解的个数
求方程$x+y+z=24$的整数解的个数,要求$1\le x\le 5,12\le y\le 18,-1\le z\le12$ 解:设$a_r$是方程$X+Y+Z=r$的满足上述要求的整数解的个数,那 ...
- 洛谷 P4097 [HEOI2013]Segment 解题报告
P4097 [HEOI2013]Segment 题目描述 要求在平面直角坐标系下维护两个操作: 在平面上加入一条线段.记第 \(i\) 条被插入的线段的标号为 \(i\) 给定一个数 \(k\),询问 ...
- CF815D Karen and Cards
CF815D Karen and Cards 固定一维c,然后(a,b)看成坐标,矩形区域求交 1.Segment tree Beats! 2.改成不合法的区域就是求并,c反向枚举,区域只增不减且完全 ...