1. 程式人生 > >ZOJ 3228 Searching the String

ZOJ 3228 Searching the String

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

題意:給一個字串s和n次詢問,每次詢問一個字串在s中出現的次數,ord為0表示允許重疊,ord為1表示不允許重疊。

 

對於允許重疊的情況,建立好自動機之後s再上面跑一邊就行了,不允許重疊需要記錄該字串上一次出現的位置,沒次與上次的位置比較判斷是否合法。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define ll long long
const int mod = 100000;
const int maxm = 600006;
const int INF = 1e9 + 7;
int n;
int tr[maxm][26], fail[maxm], last[maxm], cnt;
int  ord[maxm], id[maxm], pos[maxm], ans[maxm][2];
char str[maxm], ch[maxm];
void init()
{
	memset(last, 0, sizeof(last));
	memset(tr, 0, sizeof(tr));
	memset(fail, 0, sizeof(fail));
	memset(pos, -1, sizeof(pos));
	memset(ans, 0, sizeof(ans));
	cnt = 0;
}
int insert(char *s)
{
	int len = strlen(s);
	int now = 0;
	for (int i = 0;i < len;i++)
	{
		int num = s[i] - 'a';
		if (!tr[now][num])
			tr[now][num] = ++cnt;
		now = tr[now][num];
	}
	last[now] = len;
	return now;
}
void find_fail()
{
	int now;
	queue<int>q;
	for (int i = 0;i < 26;i++)
		if (tr[0][i]) q.push(tr[0][i]);
	while (!q.empty())
	{
		now = q.front();q.pop();
		for (int i = 0;i < 26;i++)
		{
			if (tr[now][i])
			{
				fail[tr[now][i]] = tr[fail[now]][i];
				q.push(tr[now][i]);
			}
			else tr[now][i] = tr[fail[now]][i];
		}
	}
}
void find(char *s)
{
	int p = 0, len = strlen(s);
	for (int i = 0;i < len;i++)
	{
		int j = s[i] - 'a';
		p = tr[p][j];
		if (p == 0) continue;
		int q = p;
		while (q)
		{
			if (last[q])
			{
				ans[q][0]++;
				if (i - pos[q] >= last[q])
					pos[q] = i, ans[q][1]++;
			}
			q = fail[q];
		}
	}
}
int main()
{
	int i, j, k, sum, n, cas = 0;
	while (scanf("%s", ch) != EOF)
	{
		init();
		scanf("%d", &n);
		for (i = 1;i <= n;i++)
		{
			scanf("%d%s", &ord[i], str);
			id[i] = insert(str);
		}
		find_fail();
		find(ch);
		printf("Case %d\n", ++cas);
		for (i = 1;i <= n;i++)
			printf("%d\n", ans[id[i]][ord[i]]);
		printf("\n");
	}
	return 0;
}