HDOJ 3065 病毒侵袭持续中

病毒侵袭持续中

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8885    Accepted Submission(s): 3130


Problem Description
小t非常感谢大家帮忙解决了他的上一个问题。然而病毒侵袭持续中。在小t的不懈努力下,他发现了网路中的“万恶之源”。这是一个庞大的病毒网站,他有着好多好多的病毒,但是这个网站包含的病毒很奇怪,这些病毒的特征码很短,而且只包含“英文大写字符”。当然小t好想好想为民除害,但是小t从来不打没有准备的战争。知己知彼,百战不殆,小t首先要做的是知道这个病毒网站特征:包含多少不同的病毒,每种病毒出现了多少次。大家能再帮帮他吗?
 

Input
第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。
 

Output
按以下格式每行一个,输出每个病毒出现次数。未出现的病毒不需要输出。
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。
 

Sample Input
  
  
3 AA BB CC ooxxCC%dAAAoen....END
 

Sample Output
  
  
AA: 2 CC: 1
Hint
Hit: 题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。 计数策略也可一定程度上从Sample中推测。
 

Source


#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;
const int N = 50010;

char S[2000010];

struct Trie{
	int next[N][128];
	int fail[N];
	int cnt[N];
	int root, total;

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

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

	void insert(char *str, int lct) {
		int cur = root;

		while (*str) {
			int index = *str;
			if (next[cur][index] == -1)
				next[cur][index] = new_node();
			cur = next[cur][index];
			++str;
		}
		cnt[cur] = lct;
	}

	void build() {
		fail[root] = root;
		queue<int> Q;

		for (int i = 0; i < 128; ++i) {
			if (next[root][i] != -1) {
				fail[next[root][i]] = root;
				Q.push(next[root][i]);
			}
		}

		while (!Q.empty()) {
			int cur = Q.front();
			Q.pop();

			for (int i = 0; i < 128; ++i) {
				if (next[cur][i] != -1) {
					int tmp = fail[cur];

					while (next[tmp][i] == -1 && tmp != root)
						tmp = fail[tmp];
					fail[next[cur][i]] = next[tmp][i];
					if (fail[next[cur][i]] == -1)
						fail[next[cur][i]] = root;
					Q.push(next[cur][i]);
				}
			}
		}
	}

	void query(char *str, int *T) {
		int cur = root;

		while (*str) {
			int index = *str;

			while (next[cur][index] == -1 && cur != root)
				cur = fail[cur];

			cur = next[cur][index];

			if (cur == -1)
				cur = root;
			int tmp = cur;

			while (tmp != root) {
				if (cnt[tmp]) 
					++T[cnt[tmp]];
				tmp = fail[tmp];
			}
			++str;
		}
	}

}tree;

int main() {
	int n, m;
	char str[1010][60];
	int cnt[1010];

	while (~scanf("%d", &n)) {
		tree.init();
		for (int i = 1; i <= n; ++i) {
			scanf("%s", str[i]);
			tree.insert(str[i], i);
		}

		tree.build();
		scanf("%s", S);
		memset(cnt, 0, sizeof(cnt));
		tree.query(S, cnt);

		for (int i = 1; i <= n; ++i)
			if (cnt[i])
				printf("%s: %d\n", str[i], cnt[i]);

	}

	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值