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");
}
}