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的话累计的时候加个判断:上次累计的位置与这次的距离是否达到该串长度。

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int MAX=,Tr=,LO=,NU=1e6+;
struct tree{
int w,f,id;
int t[MAX];
};
int tt,n,an[NU],iu[NU],xx[NU];
char c[LO],s[LO];
bool m[NU];
queue <int> q;
inline int f(char c){return c-'a';}
struct AC{
tree t[Tr];
int num;
int o[Tr];
inline void FI(){
register int i,j;
for (i=;i<=num;i++)
for (j=;j<MAX;j++)
t[i].t[j]=;
for (i=;i<=num;i++) t[i].w=t[i].f=t[i].id=o[i]=;
num=;
}
inline void in(int nu){
int p=,l,mm=strlen(c);iu[nu]=mm;
for (register int i=;i<mm;i++){
l=f(c[i]);
if (!t[p].t[l]) t[p].t[l]=++num;
p=t[p].t[l];
}
if (t[p].id) xx[nu]=t[p].id;else
t[p].id=nu;
}
inline void mafa(){
register int i;int k,p;
q.push();t[].f=;
while(!q.empty()){
k=q.front();q.pop();
for (i=;i<MAX;i++)
if (t[k].t[i]){
p=t[k].f;
while((!t[p].t[i])&&p) p=t[p].f;
t[t[k].t[i]].f=(k==p)?:t[p].t[i];
q.push(t[k].t[i]);
}
}
}
inline void que1(){
register int i,j;
int p=,x,mm=strlen(s);
for (i=;i<mm;i++){
x=f(s[i]);
while(!t[p].t[x]&&p) p=t[p].f;
p=t[p].t[x];
for (j=p;j;j=t[j].f) t[j].w++;
}
for (i=;i<=num;i++)
if (t[i].id) an[t[i].id]=t[i].w;
}
inline void que2(){
register int i,j;
int p=,x,mm=strlen(s);
for (i=;i<mm;i++){
x=f(s[i]);
while(!t[p].t[x]&&p) p=t[p].f;
p=t[p].t[x];
for (j=p;j;j=t[j].f)
if (t[j].id&&o[j]+iu[t[j].id]<=i+) t[j].w++,o[j]=i+;
}
for (i=;i<=num;i++)
if (t[i].id) an[t[i].id]=t[i].w;
}
}T1,T2;
int main(){
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
register int i,j;tt=;
while(~scanf("%s",&s)){
tt++;
T1.FI();T2.FI();
scanf("%d",&n);
for (i=;i<=n;i++) xx[i]=i;
for (i=;i<=n;i++){
scanf("%d",&m[i]);scanf("%s",c);
if (!m[i]) T1.in(i);else T2.in(i);
}
T1.mafa();
T2.mafa();
T1.que1();
T2.que2();
printf("Case %d\n",tt);
for (i=;i<=n;i++) printf("%d\n",an[xx[i]]);
printf("\n");
}
}

zoj 3228:Searching the String的更多相关文章

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

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

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

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

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

  4. AC自动机---Searching the String

    ZOJ   3228 题目网址:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16401 Description Little ...

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

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

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

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

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

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

  8. Searching the String - ZOJ 3228(ac自动机)

    题目大意:首先给你一下母串,长度不超过10^5,然后有 N(10^5) 次查询,每次查询有两种命令,0或者1,然后加一个子串,询问母串里面有多少个子串,0表示可以重复,1表示不可以重复.   分析:发 ...

  9. 【ZOJ 3228】Searching the String 【AC自动机】

    题意 给出n个模式串和一个文本串,输出各个模式串在文本串中出现的次数.模式串有两种类型,0类型代表可以有重叠,1类型代表不能有重叠.模式串可能出现重复. 分析 算是AC自动机的模板题? 因为模式串可以 ...

随机推荐

  1. 合并查询结果集UNION(去重), UNION ALL(不去重),INTERSECT(交集),MINUS(差集,第一个结果集减去第二个结果集,第一个结果集中不在第二个结果集中的记录行),[NOT] EXIST

    MINUS配合[NOT] EXIST使用可以查询出包含符合某个条件的多记录的其他记录, 举例: 顾客A买了商品2.4.6 顾客B买了商品1.2.4 顾客C买了商品4.6 顾客D买了商品1.2.4.6 ...

  2. sourceTree git 忽略指定文件

    按照如下步骤执行(终端命令) 1. git status modified: LovegoMall.xcworkspace/xcuserdata/Tiny.xcuserdatad/xcdebugger ...

  3. JavaWeb框架_Struts2_(五)----->Struts2的标签库

    1.  Struts2的标签库 1.1 Struts2标签库概述 Struts2的标签库可以分为以下3类:用户界面标签.非用户界面标签.AJAX标签; 2.1.1 Struts2标签库的分类和使用 1 ...

  4. javascript + sql编写SQL客户端工具tabris

    祝大家2018新年快乐, 前不久发现了一个创意的脚本JtSQL(java编写) 开源地址为:https://github.com/noear/JtSQL JtSQL 特点:*.结合了JS.SQL.模板 ...

  5. Using F2 to Rename Open Files

    Copy to your User keymap { "keys": ["shift+f2"], "command": "rena ...

  6. [编织消息框架][JAVA核心技术]annotation基础

    应用动态代理技术要先掌握annotation技术 注解是JDK1.5之后才有的新特性,JDK1.5之后内部提供的三个注解 @Deprecated 意思是“废弃的,过时的” @Override 意思是“ ...

  7. 字符串匹配KMP算法的C语言实现

    字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...

  8. 环形进度条的实现方法总结和动态时钟绘制(CSS3、SVG、Canvas)

    缘由: 在某一个游戏公司的笔试中,最后一道大题是,“用CSS3实现根据动态显示时间和环形进度[效果如下图所示],且每个圆环的颜色不一样,不需要考虑IE6~8的兼容性”.当时第一想法是用SVG,因为SV ...

  9. CSS学习之首页简单布局

    作为一个PHPer,在前端方面javascript.jquery这些的日常工作还搞的定.可对于div+css这些东西可就头疼了,所以现在开始学习CSS 跟着燕十八的教程开始从最基础学起,首先练习一个简 ...

  10. Vue 爬坑之路(九)—— 用正确的姿势封装组件

    迄今为止做的最大的 Vue 项目终于提交测试,天天加班的日子终于告一段落... 在开发过程中,结合 Vue 组件化的特性,开发通用组件是很基础且重要的工作 通用组件必须具备高性能.低耦合的特性 为了满 ...