题目链接: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自动机 + 可重叠/不可重叠的更多相关文章

  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自动机)

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

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

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

  4. 【AC自动机】zoj3228 Searching the String

    对所有模式串建立AC自动机. 每个单词结点要记录该单词长度. 然后在跑匹配的时候,对每个单词结点再处理3个值,代表可重叠的匹配次数,不可重叠的匹配次数,以及“上一次不可重叠的匹配位置”,这样结合单词长 ...

  5. ZOJ3228 Searching the String (AC自动机)

    Searching the String Time Limit: 7 Seconds                                      Memory Limit: 129872 ...

  6. HDU 6096 String (AC自动机)

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

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

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

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

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

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

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

随机推荐

  1. Graphicmagick编译

    为了需求给Graphicmagick加了几个支持,版本分别为mozjpeg2.1,bzip2-1.0.6,libpng-1.6.18,libwebp-0.4.3,mozjpeg-2.1,tiff-4. ...

  2. 控制面板的cpl程序列表

    控制面板的cpl程序列表 学习了:https://zhidao.baidu.com/question/2141898537654796628.html 最近用来sysdm.cpl: 辅助功能选项:ac ...

  3. 微信小程序 - 考试状态不同显示

    未开考 .已交卷. 考试中 .考试结束 #ddd      #f00     #ff0    默认禁用色 禁用的button仅有style起作用,四个状态,通过wx:if ... elif ... e ...

  4. vue2.0 仿手机新闻站(四)axios

    1.axios的配置 main.js import Vue from 'vue' import App from './App.vue' // 引入 路由 import VueRouter from ...

  5. struts2实现文件查看、下载

      CreateTime--2017年9月7日10:25:33 Author:Marydon struts2实现文件查看.下载 1.界面展示 <a style="color: #199 ...

  6. C语言之基本算法32—鞍点

    //数组 /* ================================================================== 题目:求随意矩阵的全部鞍点.并统计个数.(在矩阵中 ...

  7. 编译-O 选项对性能提升作用

    GCC -O 选项 这个选项控制所有的优化等级.使用优化选项会使编译过程耗费更多的时间,并且占用更多的内存,尤其是在提高优化等级的时候. -O设置一共有五种:-O0.-O1.-O2.-O3和-Os. ...

  8. 深入解析Windows窗体创建和消息分发

    Windows GUI採用基于事件驱动的编程模型,其实差点儿全部的界面库都是这样做的.在纯粹的Window32 SDK编程时代.人们还能够搞懂整个Windows窗口创建和消息的流通过程.可是在如今各种 ...

  9. redis远程登录

    #redis-cli -h 202.96.126.37 -p 6379 #auth 'd6d72fa9b2ff458e:GRjZmQ3MTN'

  10. Angular+Angular-Ui实现分页(代码更加简单,更加容易懂哦)

    前面写了个分页,但是个人认为只能玩玩,实际业务上的话代码太繁杂(笔者想走代码.性能精简化之路[不知道哪天才能成为高手~·YY一下无伤大雅]),逻辑上有点混乱.那么今天我们来看看另外一种只实现分页没有查 ...