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
/*
ZOJ 3228 Searching the String(AC自动机) 给你几个子串,然后在字符串中查询它们出现的次数.但是0表示可以重复,1表示不可以
重复. 在开始想的是建两个然后分别查询.但是发现完全可以一次查询解决 TAT
abababac
2
0 aba
1 aba 就这一组数据而言.
建成:
root
/
①a
/
②b
/
③a 对于可以重复的部分,直接进行查找就行. 因为叶子节点的a的nex[a][b]就是它的父亲
b节点
//可以参考’飘过的小牛‘的总结,主要是fail指针的理解 所以导致 a① -> b② -> a③ -> b② -> a③ 时又走到了叶子节点a.
而且只有到走到一个字符串的终点的时候才可能 +1
于是乎在每次走完一个子串的时候(通过ed判断) 判断一下它最近一次出现的位置
两个的差是否大于子串的长度即可 hhh-2016-04-26 20:19:35
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <functional>
#include <algorithm>
using namespace std; #define lson (i<<1)
#define rson ((i<<1)|1)
typedef unsigned long long ll;
typedef unsigned int ul;
const int mod = 20090717;
const int INF = 0x3f3f3f3f;
const int N = 100005*6;
int pos[100005];
char str[100005];
struct Matrix
{
int len;
int ma[111][111];
Matrix() {};
Matrix(int L)
{
len = L;
}
}; struct Tire
{
int nex[N][26],fail[N],ed[N];
int dep[N];
int tan[N][2];
int root,L;
int newnode()
{
for(int i = 0; i < 26; i++)
nex[L][i] = -1;
ed[L++] = 0;
return L-1;
} void ini()
{
L = 0;
root = newnode();
dep[root] = 0;
} int cal(char ch)
{
if(ch == 'A')
return 0;
else if(ch == 'C')
return 1;
else if(ch == 'G')
return 2;
else if(ch == 'T')
return 3;
} int inser(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++)
{
int ta = buf[i] - 'a';
if(nex[now][ta] == -1)
{
nex[now][ta] = newnode();
dep[nex[now][ta]] = i+1;
}
now = nex[now][ta];
}
ed[now] ++;
return now;
} void build()
{
queue<int >q;
fail[root] = root;
for(int i = 0; i < 26; i++)
if(nex[root][i] == -1)
nex[root][i] = root;
else
{
fail[nex[root][i]] = root;
q.push(nex[root][i]);
}
while(!q.empty())
{
int now = q.front();
q.pop();
// if(ed[fail[now]])
// ed[now] = ed[fail[now]];
for(int i = 0; i < 26; i++)
{
if(nex[now][i] == -1)
nex[now][i] = nex[fail[now]][i];
else
{
fail[nex[now][i]] = nex[fail[now]][i];
q.push(nex[now][i]);
}
}
}
} Matrix to_mat()
{
Matrix mat(L);
memset(mat.ma,0,sizeof(mat.ma));
for(int i = 0; i < L; i++)
{
for(int j = 0; j < 4; j++)
{
if(!ed[nex[i][j]])
mat.ma[i][nex[i][j]] ++;
}
}
return mat;
}
int last[N];
void query(char buf[])
{
int len = strlen(buf);
int cur = root;
memset(tan,0,sizeof(tan));
memset(last,-1,sizeof(last));
for(int i = 0;i < len;i++)
{
int ta = buf[i]-'a';
cur = nex[cur][ta];
int t = cur;
while(t != root)
{
if(ed[t])
{
tan[t][0]++;
if(i-last[t] >= dep[t])
{
last[t] = i;
tan[t][1] ++;
}
}
t = fail[t];
}
}
return ;
}
}; Tire ac;
char s[10];
int ty[100004];
int main()
{
int cas = 1;
int n;
while(scanf("%s",str) != EOF)
{
scanf("%d",&n);
ac.ini();
printf("Case %d\n",cas++);
for(int i = 0; i < n; i++)
{
scanf("%d%s",&ty[i],s);
pos[i] = ac.inser(s);
}
ac.build();
ac.query(str);
// for(int i = 0;i < n;i++)
// cout << pos[i] <<" ";
// cout <<endl;
for(int i = 0;i < n;i++)
{
printf("%d\n",ac.tan[pos[i]][ty[i]]);
}
printf("\n");
}
return 0;
}

  

ZOJ 3228 Searching the String(AC自动机)的更多相关文章

  1. 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 ...

  2. ZOJ 3228 Searching the String (AC自己主动机)

    题目链接:Searching the String 解析:给一个长串.给n个不同种类的短串.问分别在能重叠下或者不能重叠下短串在长串中出现的次数. 能重叠的已经是最简单的AC自己主动机模板题了. 不能 ...

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

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

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

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

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

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

  6. zoj 3228:Searching the String

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  7. ZOJ - 3430 Detect the Virus —— AC自动机、解码

    题目链接:https://vjudge.net/problem/ZOJ-3430 Detect the Virus Time Limit: 2 Seconds      Memory Limit: 6 ...

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

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

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

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

随机推荐

  1. mysql基础篇 - 其他基本操作

    基础篇 - 其他基本操作         其他基本操作 一.实验简介 本节实验中我们将学习并实践数据库的其他基本操作:索引.视图,导入和导出,备份和恢复等. 这些概念对于数据库管理员而言都非常重要,请 ...

  2. Codeforces 240 F. TorCoder

    F. TorCoder time limit per test 3 seconds memory limit per test 256 megabytes input input.txt output ...

  3. 集合Collection总览

    前言 声明,本文使用的是JDK1.8 从今天开始正式去学习Java基础中最重要的东西--->集合 无论在开发中,在面试中这个知识点都是非常非常重要的,因此,我在此花费的时间也是很多,得参阅挺多的 ...

  4. HashMap 的底层原理

    1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1 ...

  5. kubernetes入门(06)kubernetes的核心概念(3)

    一.API 对象 API对象是K8s集群中的管理操作单元.K8s集群系统每支持一项新功能,引入一项新技术,一定会新引入对应的API对象,支持对该功能的管理操作.例如副本集Replica Set对应的A ...

  6. kubernetes入门(02)kubernetes的架构

    一.kubernetes的主从架构 kubectl,全称 Kubernetes Control Plane,它表示Kubernetes为了实现最终的目标而构建的一组集群范围内的进程,这组进程相互协调, ...

  7. apigw鉴权分析(1-5)亚马逊 - 鉴权分析

    一.访问入口 https://developer.amazon.com/public/zh 二.鉴权方式分析 三.分解结论

  8. python网络爬虫与信息提取 学习笔记day3

    Day3: 只需两行代码解析html或xml信息    具体代码实现:day3_1    注意BeautifulSoup的B和S需要大写,因为python大小写敏感 import requests r ...

  9. Python基础--函数的嵌套和闭包

    一.名称空间和作用域 名称空间:Python所有有关命名的操作都是在操作名称空间,例如变量名,函数名 1.内置名称空间:Python解释器提供好的功能,解释器启动跟着一起启动,是全局作用域 2.全局名 ...

  10. git的理论基础

    GIT是目前世界上最先进最牛逼的分布式版本控制系统git维护的三棵树分别是工作区域.暂存区域.git仓库工作区域:就是你平时存放项目代码的地方暂存区域:用于临时存放你的改动,事实上它只是一个文件,保存 ...