ZOJ3228 Searching the String —— AC自动机 + 可重叠/不可重叠
题目链接:https://vjudge.net/problem/ZOJ-3228
Searching the String
Time Limit: 7 Seconds Memory Limit: 129872 KB
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
Hint
In Case 2,you can find the first substring starting in position (indexed from 0) 0,2,4, since they're allowed to overlap. The second substring starts in position 0 and 4, since they're not allowed to overlap.
For C++ users, kindly use scanf to avoid TLE for huge inputs.
Author: LI, Jie
Source: ZOJ Monthly, July 2009
题意:
给出一个字符串,有n次查询,每次查询为:给出一个单词,问在字符串中出现了次?并且多了一个限定:输入时,0代表单词可以在字符串中重叠,1则反之。
题解:
1.首先将这n个单词插入AC自动机中,由于输入的单词可能会重复,所以需要为单词重新编号。
2.将字符串与AC自动机进行匹配,匹配分成两类:
第一类,可重叠:与往常无异。
第二类,不可重叠:因为不可重叠,所以在匹配的过程中,需要记录此单词上一次出现的位置,记为last,当前出现单词的位置为i,单词长度为len,如果满足 i-last>=len,即表明没有与上一次出现的重叠。
注意:
C语言语法问题。当把数组以参数的形式传给函数,并且在函数内调用memset对数组进行初始化,是不能实现的,原因好像是不能知道数组的大小。
// int a[5]; 定义到此处仍然不能实现
void f(int a[]) //传数组
{
memset(a, -, sizeof(a));
for(int i = ; i<; i++)
printf("%d ", a[i]);
/*
输出为:-1 0 0 0 0
而目标输出为:-1 -1 -1 -1 -1
即使把a数组定义到f()函数上面,结果也一样。
*/
} int a[];
int main()
{
memset(a, , sizeof(a));
f(a);
}
所以,最好把数组定义到函数的可视范围内,然后直接调用。如下:
int a[];
void f() //传数组
{
memset(a, -, sizeof(a));
for(int i = ; i<; i++)
printf("%d ", a[i]);
} int main()
{
memset(a, , sizeof(a));
f();
}
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = ;
const int MAXN = 6e5+; int ans[][], type[], last[], Len[], Index[];
struct Trie
{
int sz, base;
int next[MAXN][], fail[MAXN], end[MAXN];
int root, L, id;
int newnode()
{
for(int i = ; i<sz; i++)
next[L][i] = -;
end[L++] = ;
return L-;
}
void init(int _sz, int _base)
{
sz = _sz;
base = _base;
id = L = ;
root = newnode();
}
int insert(char buf[], int id)
{
int len = strlen(buf);
int now = root;
for(int i = ; i<len; i++)
{
if(next[now][buf[i]-base] == -) next[now][buf[i]-base] = newnode();
now = next[now][buf[i]-base];
}
if(!end[now]) end[now] = ++id; //为AC自动机上的单词编号。
return end[now];
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ; i<sz; i++)
{
if(next[root][i] == -) next[root][i] = root;
else fail[next[root][i]] = root, Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i = ; i<sz; i++)
{
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
}
}
} void query(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = ; i<len; i++)
{
now = next[now][buf[i]-base];
int tmp = now;
while(tmp != root)
{
if(end[tmp]) //如果此处存在单词
{
ans[end[tmp]][]++; //可重叠
if(i-last[end[tmp]]>=Len[end[tmp]]) //不可重叠
{
ans[end[tmp]][]++;
last[end[tmp]] = i; //注意:“最后一次出现”得个概念只是相对不可重叠的而言,所以这句应该放在括号里面。
}
}
tmp = fail[tmp];
}
}
}
}; Trie ac;
char buf[], s[];
int main()
{
int n, kase = ;
while(scanf("%s", s)!=EOF)
{
scanf("%d", &n);
ac.init(,'a');
for(int i = ; i<=n; i++)
{
scanf("%d%s", &type[i], buf);
Index[i] = ac.insert(buf,i); //Index存当前单词在AC自动机上的位置。
Len[Index[i]] = strlen(buf);
}
ac.build(); memset(last, -, sizeof(last));
memset(ans, , sizeof(ans));
ac.query(s); printf("Case %d\n", ++kase);
for(int i = ; i<=n; i++)
printf("%d\n",ans[Index[i]][type[i]]);
printf("\n");
}
}
ZOJ3228 Searching the String —— AC自动机 + 可重叠/不可重叠的更多相关文章
- zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)
/** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...
- ZOJ3228 - Searching the String(AC自动机)
题目大意 给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠 题解 第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE...后来想 ...
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- 【AC自动机】zoj3228 Searching the String
对所有模式串建立AC自动机. 每个单词结点要记录该单词长度. 然后在跑匹配的时候,对每个单词结点再处理3个值,代表可重叠的匹配次数,不可重叠的匹配次数,以及“上一次不可重叠的匹配位置”,这样结合单词长 ...
- ZOJ3228 Searching the String (AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 ...
- HDU 6096 String (AC自动机)
题意:给出n个字符串和q个询问,每次询问给出两个串 p 和 s .要求统计所有字符串中前缀为 p 且后缀为 s (不可重叠)的字符串的数量. 析:真是觉得没有思路啊,看了官方题解,真是好复杂. 假设原 ...
- 【XSY3320】string AC自动机 哈希 点分治
题目大意 给一棵树,每条边上有一个字符,求有多少对 \((x,y)(x<y)\),满足 \(x\) 到 \(y\) 路径上的边上的字符按顺序组成的字符串为回文串. \(1\leq n\leq 5 ...
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- 2017多校第6场 HDU 6096 String AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
随机推荐
- ArcGIS教程:面积制表
摘要 计算两个数据集之间交叉制表的区域并输出表. 插图 使用方法 · 区域定义为输入中具有同样值的全部区.各区无需相连. 栅格和要素数据集都可用于区域输入. · 假设区域输入和类输入均为具有同样分辨率 ...
- 【Python】使用制表符换行符来添加空白
在编程中,在打印时,有时候需要显示出来的数据看着舒服一点,那么使用制表符(\t).换行符(\n)即可轻松实现 >>> print('zhangsan')zhangsan 加入制表符后 ...
- [Flex][Adobe Flash Builder 4.6]谷歌浏览器(Chrome)下运行Flex程序的问题
今天刚开始学习Flex,发现用Chrome运行程序时会一片空白,上网查了相关资料后找到了解决方法: 打开Chrome,在地址栏输入:chrome://plugins/ 进入插件管理界面,查找Fla ...
- OC中动态创建可变数组的问题.有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(要动态创建数组).两种方法
<span style="font-size:24px;">//////第一种方法 // NSMutableArray *arr = [NSMutableArray a ...
- 在Ubuntu 14.04 64bit中永久添加DNS的方法
DNS信息是由/etc/resolv.conf提供的,它是每次开机时,由/sbin/resolvconf生成的/etc/resolv.conf是/run/resolvconf/resolv.conf的 ...
- Cocos2d-x 更改文字换行风格 ( cocos2dx change line )
Cocos2dx change line 在 cocos2dx change line 的实现中,我们能够简单的使用 dimensions属性控制换行.使用它仅仅需将相应的參数值传入构造函数,或者调用 ...
- *Android 多线程下载 仿下载助手
今天带来一个多线程下载的 样例.先看一下效果.点击 下载 開始下载,同一时候显示下载进度.完成下载,变成程 安装,点击安装 提示 安装应用. 界面效果 线程池 ThreadPoolExecutor , ...
- 启动Eclipse时,启不起来JVM terminated. Exit code=-1
启动Eclipse时,启不起来JVM terminated. Exit code=-1 出现错误了,不知道什么原因原本好好的Eclipse,今天早上出问题了,启动不起来还抛出JVM terminate ...
- 常用js特效
事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); event.sr ...
- nyist oj 37 回文字符串 (动态规划经典)
回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 所谓回文字符串,就是一个字符串.从左到右读和从右到左读是全然一样的.比方"aba".当 ...