链接

这个题把病毒分为了两种,一种包含可以覆盖,另一种不可以,需要分别求出包含他们的个数,可以把两种都建在一颗tire树上,在最后求得时候判断一下当前节点是属于哪种字符串,如果是不包含的需要判断一下pre[i]+len[i]<=当前位置。

注意会有重复字符串,可以先map处理一下。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<string>
#include<map>
using namespace std;
#define N 600110
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
const int child_num = ;
char vir[];
char s[N/];
int o[N/],len[N/],ans[N/][],po[N/];
int cc[N/];
map<string,int>f;
vector<int>ed[N/];
class AC
{
private:
int ch[N][child_num];
int fail[N];
int Q[N];
int val[N];
int sz;
int id[];
public:
void init()
{
fail[] = ;
for(int i = ;i < child_num ; i++)
id[i+'a'] = i;
}
void reset()
{
memset(ch[],,sizeof(ch[]));
memset(val,,sizeof(val));
sz = ;
}
void insert(char *a,int key)
{
int p = ;
for(; *a ; a++)
{
int d= id[*a];
if(ch[p][d]==)
{
memset(ch[sz],,sizeof(ch[sz]));
ch[p][d] = sz++;
}
p = ch[p][d];
}
val[p] = key;
}
void construct()
{
int i,head=,tail = ;
for(i = ; i < child_num ;i++)
{
if(ch[][i])
{
fail[ch[][i]] = ;
Q[tail++] = ch[][i];
}
}
while(head!=tail)
{
int u = Q[head++];
for(i = ; i < child_num ; i++)
{
if(ch[u][i])
{
fail[ch[u][i]] = ch[fail[u]][i];
Q[tail++] = ch[u][i];
}
else ch[u][i] = ch[fail[u]][i];
}
}
}
void work(int m,int kk,int g)
{
memset(ans,,sizeof(ans));
memset(po,-,sizeof(po));
int p = ,i,k = strlen(s);
for(i = ; i < k ; i++)
{
int d = id[s[i]];
p = ch[p][d];
int tmp = p;
while(tmp)
{
int v = val[tmp];
ans[v][]++;
if(po[v]+len[v]<=i)
{
po[v] = i;
ans[v][]++;
}
tmp = fail[tmp];
}
}
for(i = ; i <= g ; i++)
{
for(int j = ; j < ed[i].size() ; j++)
{
int v = ed[i][j];
if(o[v]) cc[v] = ans[i][];
else cc[v] = ans[i][];
}
}
printf("Case %d\n",kk);
for(i = ; i <= m ;i++)
printf("%d\n",cc[i]);
puts("");
}
}ac;
int main()
{
int i,m,kk=;
ac.init();
while(scanf("%s",s)!=EOF)
{
ac.reset();
f.clear();
scanf("%d",&m);
int g = ;
for(i = ; i <= m; i++)
ed[i].clear();
for(i = ;i <= m ;i++)
{
scanf("%d%s",&o[i],vir);
if(!f[vir])
{
f[vir] = (++g);
ac.insert(vir,g);
len[g] = strlen(vir);
}
ed[f[vir]].push_back(i);
}
ac.construct();
kk++;
ac.work(m,kk,g);
}
return ;
}

zoj3228Searching the String(ac自动机)的更多相关文章

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

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

  2. 【XSY3320】string AC自动机 哈希 点分治

    题目大意 给一棵树,每条边上有一个字符,求有多少对 \((x,y)(x<y)\),满足 \(x\) 到 \(y\) 路径上的边上的字符按顺序组成的字符串为回文串. \(1\leq n\leq 5 ...

  3. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

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

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

  5. HDU 6096 String (AC自动机)

    题意:给出n个字符串和q个询问,每次询问给出两个串 p 和 s .要求统计所有字符串中前缀为 p 且后缀为 s (不可重叠)的字符串的数量. 析:真是觉得没有思路啊,看了官方题解,真是好复杂. 假设原 ...

  6. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

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

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

  8. NOIP模拟 string - ac自动机

    题目大意: 给n个字符串(100位以内),和一个长串s(100000位以内),求n个字符串在s中出现的次数.然后给出m次修改,每次修改s中的一个字符,对于每次修改,输出更新后的答案(修改会保存). 题 ...

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

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

  10. HDU 6086 Rikka with String AC自动机 + DP

    Rikka with String Problem Description As we know, Rikka is poor at math. Yuta is worrying about this ...

随机推荐

  1. fopen w c

    http://php.net/manual/en/function.fopen.php

  2. [daily]使用rdtsc指令,测量程序的运行速度 [转]

    原文地址:http://blog.chinaunix.net/uid-24774106-id-2779245.html 最近搞架构,一直在讨论.听人提到,自行科普了一下,先转发,mark.有机会深入学 ...

  3. 小试牛刀3之JavaScript基础题

    JavaScript基础题 1.让用户输入两个数字,然后输出相加的结果. *prompt() 方法用于显示可提示用户进行输入的对话框. 语法: prompt(text,defaultText) 说明: ...

  4. 改变UIButton 图片和文字的位置

    //设置字体和图片之间的间距 _btnLeft.titleEdgeInsets = UIEdgeInsetsMake(0, -_btnLeft.imageView.frame.size.width, ...

  5. MacOS10.11的/usr/bin目录不可写后class-dump的处理办法

    许多升级了OSX 10.11的朋友在配置class-dump的时候,会发现书上推荐的class-dump存放目录/usr/bin不再可写,如下所示: 192:~ snakeninny$ touch c ...

  6. LeetCode Zigzag Iterator

    原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...

  7. Fiddler- -Composer创建和发送HTTP Request

    Fiddler的功能还有很多, 很多功能都没有被挖掘出来.这次我们介绍Fiddler中的一个非常有用的功能Composer,是用来创建和发送HTTP Request的.Composer的使用方法很简单 ...

  8. python 安装pillow

    安装 警告 Pillow >= 2.1.0 不支持 “import _imaging”.请使用 “from PIL.Image import core as _imaging” 代替. 警告 P ...

  9. 动态链接库(DLL)总结

    以前的学习笔记,记录库的一点学习心得.主要是Windows下的静态库和动态链接库,动态链接库只写了关于非MFC的DLL,比较初级,适合和我一样的新手看看.有不对的地方请指出,有疏漏的地方请补充,如果您 ...

  10. [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...