ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU 2896

2021-05-03 14:03:21  阅读:138  来源: 互联网

标签:HDU now int 2896 next ++ include root


AC自动机应用,隔了两三天再写稍显生疏,还是需要练习提高熟练度。

自动机状态数其实是个不太好估量的数字,本题查看discuss才发现,内存会被卡

#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <deque>
using namespace std;

const int maxc= 101;
const int maxs= 58030;
const int maxv= 205;
const int maxw= 10005;
const int maxn= 500;

struct Trie
{
	int root, L;
	int next[maxs][maxc], end[maxs], fail[maxs];
	void Init()
	{
		L= 0;
		root= NewNode();
	}
	int NewNode()
	{
		for (int i= 0; i< maxc; ++i){
			next[L][i]= -1;
		}
		end[L++]= 0;
		return L-1;
	}
	void Insert(char *s, int id)
	{
		int lth= strlen(s), now= root;
		for (int i= 0; i< lth; ++i){
			int idx= s[i]-' ';
			if (-1== next[now][idx]){
				next[now][idx]= NewNode();
			}
			now= next[now][idx];
		}
		end[now]= id;
	}
	void Build()
	{
		queue<int> Q;
		fail[root]= root;
		for (int i= 0; i< maxc; ++i){
			if (-1== next[root][i]){
				next[root][i]= root;
			}
			else{
				fail[next[root][i]]= root;
				Q.push(next[root][i]);
			}
		}

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

			for (int i= 0; i< maxc; ++i){
				if (-1== next[now][i]){
					next[now][i]= next[fail[now]][i];
				}
				else{
					fail[next[now][i]]= next[fail[now]][i];
					Q.push(next[now][i]);
				}
			}
		}
	}
	int Query(char *s, int diag[])
	{
		int tot= 0, lth= strlen(s);
		int now= root;

		for (int i= 0; i< lth; ++i){
			int idx= s[i]-' ';
			now= next[now][idx];
			int tmp= now;

			while (root!= tmp){
				if (end[tmp]){
					int flag= 1;
					for (int i= 0; i< tot; ++i){
						if (diag[i]== end[tmp]){
							flag= 0;
							break;
						}
					}
					if (flag){
						diag[tot++]= end[tmp];
						if (tot>= 3){
							goto RET;
						}
					}
				}
				tmp= fail[tmp];
			}
		}

	RET: sort(diag, diag+tot);
		return tot;
	}
}ac;
char virus[maxv], web[maxw];
int diag[4];

int main(int argc, char const *argv[])
{
	int n, m;
	scanf("%d", &n);
	ac.Init();
	for (int i= 1; i<= n; ++i){
		scanf(" %s", virus);
		ac.Insert(virus, i);
	}
	ac.Build();

	scanf("%d", &m);
	int cnt= 0;
	for (int i= 1; i<= m; ++i){
		scanf(" %s", web);
		int tot= ac.Query(web, diag);
		if (tot> 0){
			++cnt;
			printf("web %d:", i);
			for (int j= 0; j< tot; ++j){
				printf(" %d", diag[j]);
			}
			putchar('\n');
		}
	}

	printf("total: %d\n", cnt);
	return 0;
}

标签:HDU,now,int,2896,next,++,include,root
来源: https://www.cnblogs.com/Idi0t-N3/p/14727349.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有