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 ...
随机推荐
- BZOJ1041 HAOI2008圆上的整点(数论)
求x2+y2=r2的整数解个数,显然要化化式子.考虑求正整数解. y2=r2-x2→y2=(r-x)(r+x)→(r-x)(r+x)为完全平方数→(r-x)(r+x)/d2为完全平方数,d=gcd(r ...
- IDEA在debug时修改变量值
IDEA在debug调试时修改变量值 例如以下代码: int y1 = 0; anchor.setDy1(y1); 在代码中,这个y1永远是0,但是y1本身是个变量 debug的时候获取到这个属性,并 ...
- 查看本地Git仓库历史修改内容
查看历史内容 在.git文件 同级目录下,右键 选择 git history 但是红框中的路径无法拷贝.右键红框中的任一文件,有 HighLight this only, Highlight this ...
- Matplotlib python 基本用法
1.简单的绘制函数 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1, 1, 50) y1 = x + 1 p ...
- apk的安装删除
1,签名: java -jar signapk.jar platform.x509.pem platform.pk8 DownloadProvider.apk DownloadProvider-sig ...
- 【模板】K短路 A-star
引理:当一个状态对应的节点第K次从堆中取出时,该状态对应的当前代价是从起点到该点的第K优解. 代码如下 /* POJ2449 */ #include <cstdio> #include & ...
- R语言 画图roc
这才是我要的滑板鞋~~~~~ #glm模型glm.model=train(y~.,data=data_train, method="glm", metric="ROC&q ...
- python os模块 常用命令
python编程时,经常和文件.目录打交道,这是就离不了os模块.os模块包含普遍的操作系统功能,与具体的平台无关.以下列举常用的命令 1. os.name()——判断现在正在实用的平台,Window ...
- on条件与where条件的区别
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1. on条件是在生成临时表时使用的条 ...
- 函数和常用模块【day04】:函数介绍(一)
本节内容 1.函数介绍 2.函数定义 3.为什么要使用函数 一.介绍 在我们以往的学习编程的过程当中,碰到的最多的两张编程方式或者说编程方法:面向过程和面向对象.其实不管是哪一种,其实都是编程的方法论 ...