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
对于那些可以重叠的串,就是模版了,对于不可重叠的,记录该节点上一次访问到时的位置即可,一开始用了vector记录每个节点结尾的串的id,匹配的时候遍历然后超时了,后来把每个串结束的节点记录下来就过了
/************************************************************************* > File Name: zoj3228.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年03月04日 星期三 16时44分33秒 ************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
const int MAX_NODE = 500100;
const int CHILD_NUM = 26;
int tag[MAX_NODE];
int num[MAX_NODE][2];
int pos[100100];
int ty[100100];
struct AC_Automation
{
int next[MAX_NODE][CHILD_NUM];
int fail[MAX_NODE];
int pre[MAX_NODE];
int size[MAX_NODE];
int root, L;
int newnode ()
{
for (int i = 0; i < CHILD_NUM; ++i)
{
next[L][i] = -1;
}
++L;
return L - 1;
}
void init ()
{
L = 0;
memset (pre, -1, sizeof(pre));
memset (num, 0, sizeof(num));
memset (size, 0, sizeof(size));
root = newnode();
size[root] = 0;
}
int Build_Trie (char buf[])
{
int now = root;
int len = strlen (buf);
for (int i = 0; i < len; ++i)
{
if (next[now][buf[i] - 'a'] == -1)
{
next[now][buf[i] - 'a'] = newnode();
}
now = next[now][buf[i] - 'a'];
size[now] = i + 1;
}
return now;
}
void Build_AC ()
{
queue <int> qu;
fail[root] = root;
for (int i = 0; i < CHILD_NUM; ++i)
{
if (next[root][i] == -1)
{
next[root][i] = root;
}
else
{
fail[next[root][i]] = root;
qu.push (next[root][i]);
}
}
while (!qu.empty())
{
int now = qu.front();
qu.pop();
for (int i = 0; i < CHILD_NUM; ++i)
{
if (next[now][i] == -1)
{
next[now][i] = next[fail[now]][i];
}
else
{
fail[next[now][i]] = next[fail[now]][i];
qu.push (next[now][i]);
}
}
}
}
void Match (char buf[])
{
int len = strlen (buf);
int now = root;
for (int i = 0; i < len; ++i)
{
now = next[now][buf[i] - 'a'];
int tmp = now;
while (tmp != root)
{
++num[tmp][0];
if (i - pre[tmp] >= size[tmp])
{
++num[tmp][1];
pre[tmp] = i;
}
tmp = fail[tmp];
}
}
}
}AC;
char buf[100010];
char str[10];
int main ()
{
int icase = 1;
while (~scanf("%s", buf))
{
AC.init();
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d%s", &ty[i], str);
pos[i] = AC.Build_Trie (str);
}
AC.Build_AC();
AC.Match(buf);
printf("Case %d\n", icase++);
for (int i = 1; i <= n; ++i)
{
printf("%d\n", num[pos[i]][ty[i]]);
}
printf("\n");
}
return 0;
}