对所有模式串建立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的更多相关文章

  1. zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)

    /** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...

  2. ZOJ3228 Searching the String —— AC自动机 + 可重叠/不可重叠

    题目链接:https://vjudge.net/problem/ZOJ-3228 Searching the String Time Limit: 7 Seconds      Memory Limi ...

  3. ZOJ3228 Searching the String (AC自动机)

    Searching the String Time Limit: 7 Seconds                                      Memory Limit: 129872 ...

  4. ZOJ3228 - Searching the String(AC自动机)

    题目大意 给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠 题解 第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE...后来想 ...

  5. ZOJ3228 Searching the String(AC自动机)

    题目大概是给一个主串,询问若干个模式串出现次数,其中有些模式串要求不能重叠. 对于可以重叠的就是一个直白的多模式匹配问题:而不可重叠,在匹配过程中贪心地记录当前匹配的主串位置,然后每当出现一个新匹配根 ...

  6. AC自动机基础知识讲解

    AC自动机 转载自:小白 还可参考:飘过的小牛 1.KMP算法: a. 传统字符串的匹配和KMP: 对于字符串S = ”abcabcabdabba”,T = ”abcabd”,如果用T去匹配S下划线部 ...

  7. ZOJ 3228 Searching the String(AC自动机)

    Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to d ...

  8. AC自动机---Searching the String

    ZOJ   3228 题目网址:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16401 Description Little ...

  9. 【ZOJ 3228】Searching the String 【AC自动机】

    题意 给出n个模式串和一个文本串,输出各个模式串在文本串中出现的次数.模式串有两种类型,0类型代表可以有重叠,1类型代表不能有重叠.模式串可能出现重复. 分析 算是AC自动机的模板题? 因为模式串可以 ...

随机推荐

  1. 使用setTimeout延时10ms执行onunloadcancel

    在做Web开发时,我们经常用到页面关闭事件onbeforeunload,可以给用户一个选择放弃关闭的机会,就比如这个博客编辑器.如果用户选择了离开,那么onunload事件自然会触发:但若用户选择了取 ...

  2. Spring源码解析-实例化bean对象

    spring加载配置文件,AbstractApplicationContext类中的refresh方法起着重要的作用. @Override public void refresh() throws B ...

  3. CSS属性中cursor:hand

    在 IE 下设置鼠标为手型的方法: cursor: hand,但是在 FIREFOX 中是无效的,解决方法是在FIREFOX中设置: cursor: pointer. 而这个pointer 值在IE和 ...

  4. iOS 全局变量设置的几种方式~

    在iOS开发过程中关于全局变量的几个方法 1. 在APPDelegate中声明并初始化全局变量.AppDelegate可以在整个应用程序中调用,在其他页面中可以使用代码段获取AppDelegate的全 ...

  5. ES6学习笔记(五)—— 编程风格

    1. 块级作用域 let 取代 var —— let 只在声明的代码块内有效,而且不存在变量提升的效用 const 取代 let —— const 比较符合函数式编程的思想,运算不改变值,只是新建值: ...

  6. Android USB Camera(1) : 调试记录【转】

    转自:http://blog.csdn.net/eternity9255/article/details/53069037 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 前言 ...

  7. ZOJ1003 Crashing Balloon

    Crashing Balloon Time Limit: 2 Seconds      Memory Limit: 65536 KB On every June 1st, the Children's ...

  8. Codeforces #107 DIV2 ABCD

    A #include <map> #include <set> #include <list> #include <cmath> #include &l ...

  9. ARM 中断状态和SVC状态的堆栈切换 (异常)【转】

    转自:http://blog.csdn.net/edwardlulinux/article/details/9261393 版权声明:本文为博主原创文章,未经博主允许不得转载. ARM 中断状态和SV ...

  10. shell 通过ANSI转换颜色

    格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[控制码" 如果单纯显示字体颜色可以固定控制码位0m. 格式: echo -e "\033[字背景 ...