ZOJ - 3228 Searching the String ac自动机重叠and不重叠匹配

一、内容

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

二、思路

  • 用last保存上一次匹配成功的位置,初始为-1, 那么当前匹配位置为i, 减去上一次匹配位置last, 如果结果>= 串的长度,那么代表和上一次匹配的串不重叠。 这时候匹配次数加1。

三、代码

#include 
#include 
#include 
using namespace std;
const int N = 1e5 + 5, M = 6 * 1e5 + 5;
int n, ty[N], T, len, tr[M][26], cnt[N][2], ne[M], fail[M], last[N], vis[N]; //last保存上一次匹配的位置
char s[N], sp[N][10];
void add(int id) {
	int p = 0;
	for (int i = 0; sp[id][i]; i++) {
		int j = sp[id][i] - 'a';
		if (!tr[p][j]) tr[p][j] = ++len;
		p = tr[p][j];
	}
	if (!fail[p]) fail[p] = id;
	else {vis[id] = fail[p];} //代表重复的串 那么直接用前面的id就行了。 
} 
void build() {
	queue<int> q;
	for (int j = 0; j < 26; j++) {
		if (tr[0][j]) q.push(tr[0][j]);  
	}
	while (!q.empty()) {
		int p = q.front(); q.pop();
		for (int j = 0; j < 26; j++) {
			int c = tr[p][j];
			if (!c) tr[p][j] = tr[ne[p]][j];
			else {
				ne[c] = tr[ne[p]][j];
				q.push(c);
			}
		}
	}
	 
}
int main() {
	while (~scanf("%s%d", s, &n)) {
		memset(tr, 0, sizeof(tr));
		memset(ne, 0, sizeof(ne));
		memset(vis, 0, sizeof(vis));
		memset(fail, 0, sizeof(fail));
		memset(last, -1, sizeof(last)); len = 0;
		memset(cnt, 0, sizeof(cnt));
		for (int i = 1; i <= n; i++) {
			scanf("%d%s", &ty[i], sp[i]); add(i);
		} 
		build();
		for (int i = 0, j = 0; s[i]; i++) {
			int k = s[i] - 'a';
			j = tr[j][k];
			int t = j;
			while (t) {
				if (fail[t]) {
					cnt[fail[t]][0]++;
                	int cha = i - last[fail[t]] - strlen(sp[fail[t]]);
					if (cha >= 0) cnt[fail[t]][1]++, last[fail[t]] = i;;
				}	
				t = ne[t];
			}
		}
		printf("Case %d\n", ++T);
		for (int i = 1; i <= n; i++) {
			if (!vis[i])printf("%d\n", cnt[i][ty[i]]);
			else printf("%d\n", cnt[vis[i]][ty[i]]);
		}
		printf("\n");
	} 
	return 0;
} 

你可能感兴趣的:(ac自动机)