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 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
随机推荐
- C# zip/unzip with ICSharpCode.SharpZipLib
download ICSharpCode and add reference using System; using System.Collections.Generic; using System. ...
- Iis load balance
http://www.agilesharp.com/u/yanyangtian/Blog.aspx/t-196 IIS负载均衡-Application Request Route详解第二篇:创建与配 ...
- 实时动态更新曲线图,x轴时间s随数据的变化而变化
$(function () { $(document).ready(function () { Highcharts.setOptions({ global: ...
- centos 下查找软件安装在哪里的命令
linux centos 下查找软件所安装的目录在哪里 1. 如果是rpm安装的可以:rpm -ql linux(1)package-name 具体你可以man rpm 2. 可以在根目录上直接fin ...
- 【重要版本】Firefly alpha beta v1.2.2 正式发布
原地址:http://bbs.gameres.com/thread_220175.html firefly 1.2.2 更新 更新内容: 功能添加 1.动态模块更新 2.部分b ...
- android 在fragment中获取界面的UI组件
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanc ...
- 李洪强iOS开发本人集成环信的经验总结_02_基本配置
李洪强iOS开发本人集成环信的经验总结_02_基本配置 来到APPdelegate中做一些配置 01 - 导入头文件 02 - 在didFinishLaunchingWithOptions用法总结 ...
- Java 数组倒序
String s[] = {,,,,,,}; ; i < s.length; i ++){ System.-i] +","); }
- laravel route路由,视图和response和filter
Laravel充分利用PHP 5.3的特性,使路由变得简单并富于表达性.这使得从构建API到完整的web应用都变得尽可能容易.路由的实现代码在 application/routes.php 文件. 和 ...
- Android 内核初识(3)init进程
init是一个进程,确切地说,它是Linux系统中用户空间的第一个进程.由于Android是基于Linux内核的,所以init也是Android系统中用户空间的第一个进程,它的进程号是1.作为天字第一 ...