【AC自动机】zoj3228 Searching the String
对所有模式串建立AC自动机。
每个单词结点要记录该单词长度。
然后在跑匹配的时候,对每个单词结点再处理3个值,代表可重叠的匹配次数,不可重叠的匹配次数,以及“上一次不可重叠的匹配位置”,这样结合单词长度就能保证不重叠。有多个重叠时,取靠前的位置更优。
Update:加了个优化,仅当某个结点的字符串的后缀可能是单词的时候,才会顺着fail指针往回跳。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
queue<int>q;
bool word[601000];
int child[601000][26],fail[601000],size,pos[101000];
int cnta[601000],cntb[601000],posn[601000],lens[601000];
void Insert(char S[],int id)
{
int len=strlen(S);
int now=0;
for(int i=0;i<len;++i)
{
if(!child[now][S[i]-'a'])
child[now][S[i]-'a']=size++;
now=child[now][S[i]-'a'];
}
word[now]=1;
lens[now]=len;
pos[id]=now;
}
void build()
{
fail[0]=-1;
q.push(0);
while(!q.empty())
{
int U=q.front(); q.pop();
for(int i=0;i<26;++i)
if(child[U][i])
{
int V=fail[U];
while(V!=-1)
{
if(child[V][i])
{
fail[child[U][i]]=child[V][i];
break;
}
V=fail[V];
}
if(V==-1)
fail[child[U][i]]=0;
if(word[fail[child[U][i]]])
word[child[U][i]]=1;
q.push(child[U][i]);
}
}
}
void match(char S[])
{
int len=strlen(S);
int res=0,now=0;
for(int i=0;i<len;++i)
{
if(child[now][S[i]-'a'])
now=child[now][S[i]-'a'];
else
{
int U=fail[now];
while(U!=-1 && child[U][S[i]-'a']==0)
U=fail[U];
if(U==-1)
now=0;
else
now=child[U][S[i]-'a'];
}
if(!word[now])
continue;
int U=now;
while(U)
{
if(word[U])
{
++cnta[U];
if(posn[U]<=i-lens[U])
{
++cntb[U];
posn[U]=i;
}
}
U=fail[U];
}
}
}
void Init()
{
memset(child,0,sizeof(child));
memset(fail,0,sizeof(fail));
memset(word,0,sizeof(word));
memset(pos,0,sizeof(pos));
memset(cnta,0,sizeof(cnta));
memset(cntb,0,sizeof(cntb));
memset(posn,-1,sizeof(posn));
memset(lens,0,sizeof(lens));
size=1;
}
char s[101000];
bool op[101000];
int n;
int main()
{
char ss[9];
int T=0;
//freopen("zoj3228.in","r",stdin);
while(scanf("%s%d",s,&n)!=EOF)
{
printf("Case %d\n",++T);
Init();
for(int i=1;i<=n;++i)
{
scanf("%d%s",&op[i],ss);
Insert(ss,i);
}
build();
match(s);
for(int i=1;i<=n;++i)
printf("%d\n",op[i] ? cntb[pos[i]] : cnta[pos[i]]);
puts("");
}
return 0;
}
【AC自动机】zoj3228 Searching the String的更多相关文章
- 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自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 ...
- ZOJ3228 - Searching the String(AC自动机)
题目大意 给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠 题解 第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE...后来想 ...
- ZOJ3228 Searching the String(AC自动机)
题目大概是给一个主串,询问若干个模式串出现次数,其中有些模式串要求不能重叠. 对于可以重叠的就是一个直白的多模式匹配问题:而不可重叠,在匹配过程中贪心地记录当前匹配的主串位置,然后每当出现一个新匹配根 ...
- AC自动机基础知识讲解
AC自动机 转载自:小白 还可参考:飘过的小牛 1.KMP算法: a. 传统字符串的匹配和KMP: 对于字符串S = ”abcabcabdabba”,T = ”abcabd”,如果用T去匹配S下划线部 ...
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- AC自动机---Searching the String
ZOJ 3228 题目网址:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16401 Description Little ...
- 【ZOJ 3228】Searching the String 【AC自动机】
题意 给出n个模式串和一个文本串,输出各个模式串在文本串中出现的次数.模式串有两种类型,0类型代表可以有重叠,1类型代表不能有重叠.模式串可能出现重复. 分析 算是AC自动机的模板题? 因为模式串可以 ...
随机推荐
- [hdu 4348]区间修改区间查询可持久化线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 一开始把lazy标记给push_down了,后来发现这样会让持久化变乱,然后想到不用push_d ...
- java md5加密 不依赖base64包
/** * MD5 加密 */ private String getMD5Str(String str) { MessageDigest messageDigest = null; try { mes ...
- [BZOJ1024][SCOI2009]生日快乐解题报告
Description windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕.现在包括windy,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的蛋 ...
- Mysql TEXT类型长度
BLOBTEXT一个BLOB或TEXT列,最大长度为65535(2^16-1)个字符. MEDIUMBLOBMEDIUMTEXT一个BLOB或TEXT列,最大长度为16777215(2^24-1)个字 ...
- hdu 2112 HDU Today (最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给出起点和终点,然后算出最短的路. 不过有好多细节要注意: (1)起始点和终止点相等的 ...
- 关于might_sleep的一点说明---CONFIG_DEBUG_ATOMIC_SLEEP【转】
转自:http://blog.chinaunix.net/uid-23769728-id-3157536.html 这个函数我在看代码时基本上是直接忽略的(因为我知道它实际上不干什么事),不过因为内核 ...
- shell浅谈之三for、while、until循环【转】
转自:http://blog.csdn.net/taiyang1987912/article/details/38929069 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[- ...
- js向标签中添加文本或其他的简例
1.如何用js 在div内插入内容? 不是改变内容,而是插入,就是在保留原内容的基础上,在尾部添加.举个例子. 元内容<div>你好</div> 插入后<div>你 ...
- solr requestHandler
使用哪一个handler: 1. 可以通过在url中追加有名字的handler(以 ' / ' 开头命名)的名称来指定使用哪一个handler. 如: <requestHandler name= ...
- python模拟鼠标和键盘操作
import win32api import win32con import win32gui from ctypes import * import time VK_CODE = { 'backsp ...