ZOJ - 3228 Searching the String (AC自己主动机)
Description
Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave jay another problem, poor jay finally broke out and cried, " Who
can help me? I'll bg him! "
So what is the problem this time?
First, moondy gave jay a very long string A. Then she gave him a sequence of very short substrings, and asked him to find how many times each substring appeared in string A. What's more, she would denote whether or not founded appearances
of this substring are allowed to overlap.
At first, jay just read string A from begin to end to search all appearances of each given substring. But he soon felt exhausted and couldn't go on any more, so he gave up and broke out this time.
I know you're a good guy and will help with jay even without bg, won't you?
Input
Input consists of multiple cases( <= 20 ) and terminates with end of file.
For each case, the first line contains string A ( length <= 10^5 ). The second line contains an integer N ( N <= 10^5 ), which denotes the number of queries. The next N lines, each with an integer type and a string a (
length <= 6 ), type = 0 denotes substring a is allowed to overlap and type = 1 denotes not. Note that all input characters are lowercase.
There is a blank line between two consecutive cases.
Output
For each case, output the case number first ( based on 1 , see Samples ).
Then for each query, output an integer in a single line denoting the maximum times you can find the substring under certain rules.
Output an empty line after each case.
Sample Input
ab
2
0 ab
1 ab abababac
2
0 aba
1 aba abcdefghijklmnopqrstuvwxyz
3
0 abc
1 def
1 jmn
Sample Output
Case 1
1
1 Case 2
3
2 Case 3
1
1
0
题意:先给你一个字符串,然后给你若干个子串,0代表能够重叠,1代表不能重叠,求出如今母串的次数。
思路:AC自己主动机,多一个推断的是假设这个子串与上次出现的次数大于子串长度的话。就代表这次不是重叠的了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
typedef long long ll;
using namespace std;
const int maxn = 600010;
int wordend[100010];
struct Trie {
int nxt[maxn][26], fail[maxn], deep[maxn];
int root, sz;
int cnt[maxn][2], last[maxn];
int newNode() {
for (int i = 0; i < 26; i++)
nxt[sz][i] = -1;
deep[sz++] = 0;
return sz - 1;
}
void init() {
sz = 0;
root = newNode();
}
void insert(char str[], int num) {
int u = root;
for (int i = 0; str[i]; i++) {
int tmp = str[i] - 'a';
if (nxt[u][tmp] == -1)
nxt[u][tmp] = newNode();
deep[nxt[u][tmp]] = deep[u] + 1;
u = nxt[u][tmp];
}
wordend[num] = u;
}
void build() {
queue<int> q;
fail[root] = root;
int u = root;
for (int i = 0; i < 26; i++) {
if (nxt[u][i] == -1)
nxt[u][i] = root;
else {
fail[nxt[u][i]] = root;
q.push(nxt[u][i]);
}
}
while (!q.empty()) {
u = q.front();
q.pop();
for (int i = 0; i < 26; i++) {
if (nxt[u][i] == -1)
nxt[u][i] = nxt[fail[u]][i];
else {
fail[nxt[u][i]] = nxt[fail[u]][i];
q.push(nxt[u][i]);
}
}
}
}
void query(char *buf) {
for (int i = root; i < sz; i++) {
cnt[i][0] = 0;
cnt[i][1] = 0;
last[i] = -1;
}
int u = root;
for (int i = 0; buf[i]; i++) {
u = nxt[u][buf[i]-'a'];
int tmp = u;
while (tmp != root) {
cnt[tmp][0]++;
if (i - last[tmp] >= deep[tmp]) {
cnt[tmp][1]++;
last[tmp] = i;
}
tmp = fail[tmp];
}
}
}
} ac;
char buf[100010], word[10];
int type[100010];
int main() {
int n, cas = 1;
while (scanf("%s", buf) != EOF) {
scanf("%d", &n);
ac.init();
for (int i = 0; i < n; i++) {
scanf("%d%s", &type[i], word);
ac.insert(word, i);
}
ac.build();
ac.query(buf);
printf("Case %d\n", cas++);
for (int i = 0; i < n; i++)
printf("%d\n", ac.cnt[wordend[i]][type[i]]);
printf("\n");
}
return 0;
}
ZOJ - 3228 Searching the String (AC自己主动机)的更多相关文章
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- ZOJ 3228 Searching the String (AC自己主动机)
题目链接:Searching the String 解析:给一个长串.给n个不同种类的短串.问分别在能重叠下或者不能重叠下短串在长串中出现的次数. 能重叠的已经是最简单的AC自己主动机模板题了. 不能 ...
- ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)
题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...
- zoj 3228:Searching the String
Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...
- Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)
标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...
- zoj 3430 Detect the Virus(AC自己主动机)
题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...
- zoj 3430 Detect the Virus(AC自己主动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- AC自己主动机
AC自己主动机 AC自己主动机是KMP和Trie的结合,主要处理多模板串匹配问题.以下推荐一个博客,有助于学习AC自己主动机. NOTONLYSUCCESS 这里另一个Kuangbin开的比赛,大家 ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
随机推荐
- uReplicator实现分析
MirrorMakerWorker分析 是整个同步机制的主入口,主要组织的逻辑有: 配置数据的传入与处理,ConsumerConfig对象的构建 度量对象的准备,定时上报的度量数据收集线程的定义与启动 ...
- 深入学习之mysql(一)数据库操作
1.显示所有数据库: SHOW DATABASES; 2.创建数据库: CREATE DATABASE 数据库名: 3.选择你所创建的数据库: USE 数据库名; 4.删除数据库: DROP 数据库名 ...
- SQL SERVER存储引擎——04.数据
4. SQL SERVER存储引擎之数据篇 (4.1)文件 (0)主数据文件.mdf初始文件大小至少为3MB,次要数据文件.ndf初始大小,同日志文件一样至少为512KB: (1)SQL SERVER ...
- 观15级K班团队作业有感
1.指尖加密 特点:通过可移动设备手机参与电脑文件的解密,使加密更加安全. 缺点:跟柯逍老师的想法差不多,UI简陋,操作不是很友好,或许可以加一个帮助文档. 2.youreyes 特点:可以检测路过的 ...
- Web进程被kill掉后线程还在运行怎么办?
目录 背景描述 原因分析 处理方案 参考 背景描述 系统有一个配置表,系统在启动后会启动一个线程,每隔5分钟将配置表里所有的数据更新到内存中. 系统是通过jenkins构建(直接kill掉Web进程, ...
- js深度复制
项目过程遇到需要对一个对象处理,然后独立出来用,结果怎么处理都会影响到原有变量,原来是引用导致,只有深度复制才行. 最终用下面的赋值方法才搞成功 var result=$.extend( true, ...
- Redis实现缓存,你应该懂的哪些思路!
场景一:类似于微博,实现关注和被关注功能. 思路: 对每个用户使用两个集合类型键,用来存储关注别人的用户和被该用户关注的用户.当用户A关注用户B的时候,执行两步操作: sadd user:A B sa ...
- 【bzoj3280】小R的烦恼 费用流
题目描述 小R最近遇上了大麻烦,他的程序设计挂科了.于是他只好找程设老师求情.善良的程设老师答应不挂他,但是要求小R帮助他一起解决一个难题. 问题是这样的,程设老师最近要进行一项邪恶的实验来证明P=N ...
- Sum of Squares of the Occurrence Counts解题报告(后缀自动机+LinkCutTree+线段树思想)
题目描述 给定字符串\(S(|S|\le10^5)\),对其每个前缀求出如下的统计量: 对该字符串中的所有子串,统计其出现的次数,求其平方和. Sample Input: aaa Sample Out ...
- Codeforces Beta Round #95 (Div. 2) C 组合数学
C. The World is a Theatre time limit per test 2 seconds memory limit per test 256 megabytes input st ...