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 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
随机推荐
- linux中判断符号[]注意事项
1.中括号[]内的每个组件都需要有空格键来分割: 2.在中括号内的变量,最好都一双引号括号起来: 3.在中括号内的常量,最好都以单引号或双引号括号起来.
- Python setup.py和MANIFEST.in文件
Setup.py文件 from setuptools import setup from codecs import open # 第三方依赖包及版本号 requires = ['beautifuls ...
- EVB-P6UL:一识庐山真面目
前言 原创文章,转载引用务必注明链接.水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验与正确的图片链接显示,请访问我的博客原文: 在爱板网上看到这个活动,昨晚确认,今 ...
- Linux测网速
$ wget https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py$ chmod a+rx speedtest_cli. ...
- VueJS字符串反转:String.reverse()
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- sql 查询 一张表里面的数据 在另一张表中是否存在 和 比对两个集合中的差集和交集(原创)
这两天在搞一个修复的小功能 需求: A表,B表,C表,日志文件 先筛选出A表和B表中都符合条件的数据,然后检查这些数据在C表中是否存在.如果不存在,就从日志中读取数据,存入C表中,如果存在,则不做操作 ...
- spring-web中的StringHttpMessageConverter简介
spring的http请求内容转换,类似netty的handler转换.本文旨在通过分析StringHttpMessageConverter 来初步认识消息转换器HttpMessageConverte ...
- 【甘道夫】Ubuntu14 server + Hadoop2.2.0环境下Sqoop1.99.3部署记录
第一步.下载.解压.配置环境变量: 官网下载sqoop1.99.3 http://mirrors.cnnic.cn/apache/sqoop/1.99.3/ 将sqoop解压到目标文件夹,我的是 /h ...
- log4j入门及常用配置
<pre class="java" name="code">import org.apache.log4j.BasicConfigurator; ...
- ubuntu boost.python
安装boost(未尝试只安装 libboost-python-dev) sudo apt-get install libboost-all-dev 新建hello_ext.cpp,输入以下代码 1 c ...