对所有模式串建立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. 用@Component注解代替@Configuration注解,定义bean

    package com.timo.entity; import org.springframework.beans.factory.annotation.Value; import org.sprin ...

  2. Spring - IoC(5): 集合属性的注入

    如果 Bean 的属性是个集合,则可以使用 <list/>.<set/>.<map/> 和 <props/> 元素向 List.Set.Map 和 Pr ...

  3. 计算机网络中七层,五层,四层协议;IP 地址子网划分

    七层协议: 7 应用层(http) 6 表示层(上层用户可以相互识别的数据:jpg) 5 会话层(不同主机不同线程间的通信) 4 运输层(tcp/ip:传输层提供端到端的透明数据服务)/差错控制和流量 ...

  4. 【洛谷 P4168】[Violet]蒲公英(分块)

    题目链接 题目大意:给定\(n\)个数和\(m\)个求区间众数的询问,强制在线 这题我\(debug\)了整整一个下午啊..-_- 从14:30~16:45终于\(debug\)出来了,\(debug ...

  5. Nginx的主要配置参数说明

    #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | in ...

  6. LCD实验学习笔记(四):系统时钟

    一般CPU频率(FCLK)高于内存.网卡等设备频率(HCLK),而串口.USB.I2C等设备频率(PCLK)更低. 系统时钟: 系统时钟源为晶振,初始频率12MHz. 通过设置MPLLCON寄存器的M ...

  7. 解决小米/红米手机无法进行jdwp调试的问题

    问题描述:在逆向一个app,研究环境是一台红米2,需要使用jdwp接口,也就是ddms下面这个界面: 但神奇的是,同一台主机上,模拟器的进程可以显示在ddms界面上,红米2确一个进程都没有显示出来.c ...

  8. 简单粗暴的webapp语言国际化

    不同语言以json格式存放不同文件 { "information": "个人资料", "fuckworld":"你好世界" ...

  9. string与double的互相转换

    #include <iostream> #include <string> #include <sstream> string DoubleToString(dou ...

  10. OC学习篇之---代理模式

    何实现代理模式的. 这里举一个简单的例子: 小孩类,护士类,保姆类,其中小孩类有两个方法:wash和play 这里代理对象就是:护士类.保姆类,小孩类是被代理对象. 看一下代码: 首先看一下小孩类: ...