hdu2896

	/**/
	#include <cstdio>
	#include <cstring>
	#include <cmath>
	#include <cctype>
	#include <iostream>
	#include <algorithm>
	#include <map>
	#include <set>
	#include <vector>
	#include <string>
	#include <stack>
	#include <queue>

	typedef long long LL;
	using namespace std;

	int n, m;
	char s[205];
	char str[10005];
	int ans, an[1005], tt;

	struct AC_Automaton
	{
		int ch[100005][130], fail[100005], id[100005];
		int tot, root;

		int newnode(){
			for (int i = 0; i < 130; i++){
				ch[tot][i] = -1;
			}
			id[tot++] = 0;
			return tot - 1;
		}

		void init(){
			tot = 0;
			root = newnode();
		}

		void insert(char *ss, int cnt){
			int p = 0, len = strlen(ss);
			for (int i = 0; i < len; i++){
				if(ch[p][ss[i]] == -1){
					ch[p][ss[i]] = newnode();
				}
				p = ch[p][ss[i]];
			}
			id[p] = cnt;
		}

		void build(){
			int l = 0, r = 0, q[100005];
			for (int i = 0; i < 130; i++){
				if(ch[0][i] == -1){
					ch[0][i] = 0;
				}else{
					fail[ch[0][i]] = 0;
					q[r++] = ch[0][i];
				}
			}
			while(l < r){
				int p = q[l++];
				for (int i = 0; i < 130; i++){
					if(ch[p][i] == -1){
						ch[p][i] = ch[fail[p]][i];
					}else{
						fail[ch[p][i]] = ch[fail[p]][i];
						q[r++] = ch[p][i];
					}
				}
			}
		}

		void count(char *ss){
			int p = 0, len = strlen(ss);
			for (int i = 0; i < len; i++){
				p = ch[p][ss[i]];
				int temp = p;
				while(temp){
					if(id[temp]){
						an[tt++] = id[temp];
					}
					temp = fail[temp];
				}
			}
		}
	}AC;

	int main()
	{
		//freopen("in.txt", "r", stdin);
		//freopen("out.txt", "w", stdout);

		while(scanf("%d", &n) == 1){
			AC.init();
			ans = 0, tt = 0;
			for (int i = 1; i <= n; i++){
				scanf("%s", s);
				AC.insert(s, i);
			}
			AC.build();
			scanf("%d", &m);
			for (int i = 1; i <= m; i++){
				scanf("%s", str);
				tt = 0;
				AC.count(str);
				if(tt){
					printf("web %d:", i);
					sort(an, an + tt);
					for (int i = 0; i < tt; i++){
						printf(" %d", an[i]);
					}
					printf("\n");
					ans++;
				}
			}
			printf("total: %d\n", ans);
		}

		return 0;
	}
	/**/

没搞懂为啥memset就超空间了,还要写个newnode初始化

全部评论

相关推荐

10-05 11:11
海南大学 Java
投票
理想江南137:感觉挺真诚的 感觉可以试一试
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 10:48
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务