ZOJ3228 - Searching the String(AC自动机)
题目大意
给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠
题解
第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE。。。后来想想其实只要跑一次文本串即可。。。
这个题目主要的问题是要解决有可重叠和不可重叠两种情况,用一个数组记录下模式串上一次出现的位置就可以判断是否重叠了
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
#define MAXN 100005
const int maxnode=600005;
const int sigma_size=26;
char T[MAXN],s[7];
bool flag[MAXN];
int pos[MAXN];
struct Triegragh
{
int ch[maxnode][sigma_size];
int fail[maxnode];
int deep[maxnode];
int cnt[maxnode][2];
int end[maxnode];
int last[maxnode];
int val[maxnode];
int sz;
void init()
{
sz=1;
memset(ch[0],0,sizeof(ch[0]));
memset(cnt,0,sizeof(cnt));
memset(end,-1,sizeof(end));
memset(val,0,sizeof(val));
}
int idx(char c){return c-'a';}
int insert(char *s)
{
int u=0,n=strlen(s);
for(int i=0;i<n;i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
deep[sz]=i+1;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]++;
return u;
}
void getfail()
{
queue<int>q;
fail[0]=0;
for(int c=0;c<sigma_size;c++)
{
int u=ch[0][c];
if(u){fail[u]=0;q.push(u);last[u]=0;}
}
while(!q.empty())
{
int r=q.front();q.pop();
for(int c=0;c<sigma_size;c++)
{
int u=ch[r][c];
if(!u){ch[r][c]=ch[fail[r]][c];continue;}
q.push(u);
fail[u]=ch[fail[r]][c];
last[u]=val[fail[u]]?fail[u]:last[fail[u]];
}
}
}
void count(int j,int i)
{
if(j)
{
cnt[j][0]++;
if(i-end[j]>=deep[j])
{
cnt[j][1]++;
end[j]=i;
}
count(last[j],i);
}
}
void find(char *T)
{
int j=0,n=strlen(T);
for(int i=0;i<n;i++)
{
int c=idx(T[i]);
j=ch[j][c];
if(val[j])
count(j,i);
else
if(last[j]) count(last[j],i);
}
}
};
Triegragh ac;
int main()
{
int kase=0;
while(scanf("%s",T)!=EOF)
{
printf("Case %d\n",++kase);
int n;
scanf("%d",&n);
ac.init();
for(int i=1;i<=n;i++)
{
scanf("%d%s",&flag[i],s);
pos[i]=ac.insert(s);
}
ac.getfail();
ac.find(T);
for(int i=1;i<=n;i++)
printf("%d\n",ac.cnt[pos[i]][flag[i]]);
printf("\n");
}
return 0;
}
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 ...
- 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 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
随机推荐
- css3 Transition动画执行时有可能会出现闪烁的bug
css3 Transition动画执行时有可能会出现闪烁的bug,一般出现在开始的时候. 解决方法: 1.-webkit-backface-visibility: hidden; 2.-webkit- ...
- nutch 采集到的数据与实际不符
现象,这个网站我总计能抽取将近500个URL,但实际只抽取了100条 解析:nutch默认从一个页面解析出的链接,只取前 100 个. <property> <name>db. ...
- [转载]C# Random 生成不重复随机数
Random 类 命名空间:System 表示伪随机数生成器,一种能够产生满足某些随机性统计要求的数字序列的设备. 伪随机数是以相同的概率从一组有限的数字中选取的.所选数字并不具有完全的随机性,因为它 ...
- python re.sub
python re.sub python re.sub属于python正则的标准库,主要是的功能是用正则匹配要替换的字符串然后把它替换成自己想要的字符串的方法下面给个例子:import relin ...
- Node.js tools for visual studio 在vs中使用Node.js
简单介绍 PTVS开发团队又开发出一款可以在VS里编写Node.js应用程序的插件——NTVS(Node.js Tools for Visual Studio),开发者可以在VS里轻松开发Node.j ...
- shell脚本ssh自动登陆服务器
#!/bin/shsshpass -p "your password" ssh user@ip 注意: 需要先安装sshpass: 源码下载地址:http://sourceforg ...
- MinGW 编译libwebsockets
libwebsockets是一个轻量的纯C库,在这里尝试使用MinGW进行构建. 官网地址:http://libwebsockets.org/trac/libwebsockets下载地址:http:/ ...
- webstore+nodejs
新建一个普通的project. 编写如下代码: var http=require('http'); http.createServer(function(req,res){ res.writeHead ...
- rails3 Bundle简介
Rails 3开始使用bundle来管理项目的gem依赖.该命令只在一个含有Gemfile的目录下执行. Gemfile: Rails 项目所有的依赖包都在这里配置,不像以前通过require来查找 ...
- Understanding Item Import and Debugging Problems with Item Import (Doc ID 268968.1)
In this Document Purpose Details Scenario 1: Testing the basic item import with minimum columns po ...