ZOJ 3228 Searching the String(AC自动机)

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

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.


题意:给定一个10^5的字符串,然后给出10^5个长度不超过6的字符串, 问给出短字符串在长字符串中出现了几次?当短字符串前的数字为0的时候, 字符串可以在长字符串中重复, 当为1的时候, 不允许重复.

题解:当前面的数字为1的时候, 普通的AC自动机就可以搞, 当前面为0的时候, 我们可以用last数组标记上一个单词出现的位置, 这样就可以看可不可以得到了.

我来解释下程序当中的数组:

pos[i]: 第i个字符串在AC自动机当中的位置

state[i]: 第i个字符串的状态

cnt[i][0]: 第i个字符串状态为0时在长字符串中出现的次数

cnt[i][1]: 第i个字符串状态为1时在长字符串中出现的次数

deep[i]: AC自动机中节点i的深度

last[i]: AC自动机中以第i个节点为结尾的单词在长字符串中出现的位置

当遍历到第k个位置时, 如果k-deep[i] >=last[i], 那么, cnt[i][1]就可以增加一个了.



#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
const int N = 600010;
const int M = 100010;
const int L = 26;

char str[M];
int pos[M], state[M];

struct Trie{
	int next[N][L], fail[N], deep[N];
	int cnt[N][2], last[N];
	int total, root;

	int new_node() {
		for (int i = 0; i < L; ++i)
			next[total][i] = -1;
		deep[total] = -1;
		last[total] = -1;
		cnt[total][0] = cnt[total][1] = 0;
		return total++;
	}

	void init() {
		total = 0;
		root = new_node();
	}

	int insert(char *str) {
		int cur = root;
		for (int i = 0; str[i]; ++i) {
			int idx = str[i] - 'a';
			if (next[cur][idx] == -1)
				next[cur][idx] = new_node();
			cur = next[cur][idx];
		}
		deep[cur] = strlen(str);
		return cur;
	}

	void build() {
		fail[root] = -1;
		queue<int> q;
		q.push(root);

		while (!q.empty()) {
			int cur = q.front();
			q.pop();
			for (int i = 0; i < L; ++i) {
				if (next[cur][i] != -1) {
					int tmp = fail[cur];
					while (tmp != -1 && next[tmp][i] == -1)
						tmp = fail[tmp];
					fail[next[cur][i]] = tmp == -1 ? root : next[tmp][i];
					q.push(next[cur][i]);
				}
			}
		}
	}

	void query() {
		int cur = root;

		for (int i = 0; str[i]; ++i) {
			int idx = str[i] - 'a';

			while (cur != -1 && next[cur][idx] == -1)
				cur = fail[cur];
			cur = cur == -1 ? root : next[cur][idx];

			int tmp = cur;

			while (tmp != root) {
				if (deep[tmp > 0]) {
					++cnt[tmp][0];
					if (i - deep[tmp] >= last[tmp]) {
						last[tmp] = i;
						++cnt[tmp][1];
					}
				}
				tmp = fail[tmp];
			}
		}
	}

}tree;

int main() {
	while (~scanf("%s", str)) {
		int n;
		scanf("%d", &n);
		char buf[10];

		tree.init();
		for (int i = 0; i < n; ++i) {
			scanf("%d%s", state + i, buf);
			pos[i] = tree.insert(buf);
		}
		tree.build();
		tree.query();
		static int Cnt = 0;
		printf("Case %d\n", ++Cnt);
		for (int i = 0; i < n; ++i)
			printf("%d\n", tree.cnt[pos[i]][state[i]]);
		puts("");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值