HDU 2222 AC自动机

/****************************************************************************************************
    HDU最2的题目~AC自动机模板,根据Racebug的模板改的,结果改了下140ms过的~YM Racebug~
****************************************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
//#include <hash_map>		//Visual C++ lib
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )
#define CLR(x, k) memset((x), (k), sizeof(x))
#define CPY(t, s) memcpy((t), (s), sizeof(s))
#define LEN(s) static_cast<int>( strlen((s)) )

typedef long long LL;		//GNU C++
typedef unsigned long long ULL;
//typedef __int64 LL;		//Visual C++ 2010
//typedef unsigned __int64 ULL;
typedef double LF;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;

//typedef hash_map<int, int>::iterator HMI;
typedef map<int, int>::iterator MI;
typedef vector<int>::iterator VI;
typedef list<int>::iterator LI;
typedef set<int>::iterator SI;

const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL;		//15f
const double oo = 10e9;
const double eps = 10e-9;
const double PI = acos(-1.0);

const int MAXN = 500004;
const int MAXC = 26;

int test, n;
char str[MAXN * 2];

struct Node
{
	Node *next[MAXC];
	Node *fail;
	int cnt;
}trie[MAXN], *root;
int ncnt;
queue<Node *>Q;

void initTrie()
{
	root = trie;
	ncnt = 1;
	for (int i = 0; i < MAXC; ++i)
	{
		root->next[i] = NULL;
	}
	root->fail = NULL;
	root->cnt = 0;
	return ;
}
Node* createNode(Node *crt)
{
	for (int i = 0; i < MAXC; ++i)
	{
		crt->next[i] = NULL;
	}
	crt->fail = NULL;
	crt->cnt = 0;
	return crt;
}
void insert(char *ch)
{
	Node *ptr = root;
	int nxt;
	while (*ch != '\0')
	{
		nxt = *ch - 'a';
		if (NULL == ptr->next[nxt])
		{
			ptr->next[nxt] = createNode( &trie[ncnt++] );
		}
		ptr = ptr->next[nxt];
		++ch;
	}
	++ptr->cnt;
	return ;
}
void buildAC_automation()
{
	while (!Q.empty())
	{
		Q.pop();
	}
	Q.push(root);
	Node *crt, *nxt, *ptr;
	while (!Q.empty())
	{
		crt = Q.front();
		Q.pop();
		for (int i = 0; i < MAXC; ++i)
		{
			nxt = crt->next[i];
			if (nxt != NULL)
			{
				for (ptr = crt->fail; ptr != NULL; ptr = ptr->fail)
				{
					if (ptr->next[i] != NULL)
					{
						nxt->fail = ptr->next[i];
						//nxt->cnt += nxt->fail->cnt; //
						break ;
					}
				}
				if (NULL == ptr)
				{
					nxt->fail = root;
				}
				Q.push(nxt);
			}
			else
			{
				if (root == crt)
				{
					crt->next[i] = root;
				}
				else
				{
					crt->next[i] = crt->fail->next[i];
				}
			}
		}
	}
	return ;
}
int query(char *str)
{
	int res = 0, id;
	Node *ptr = root, *ptt;
	while (*str != '\0')
	{
		id = *str - 'a';
		ptr = ptr->next[id];
		for (ptt = ptr; ptt != NULL && ptt->cnt != -1; ptt = ptt->fail)
		{
			res += ptt->cnt;
			ptt->cnt = -1;
		}
		++str;
	}
	return res;
}
void ace()
{
	char ch[52];
	for (scanf("%d", &test); test--; )
	{
		scanf("%d%*c", &n);
		initTrie();
		for (int i = 0; i < n; ++i)
		{
			gets(ch);
			insert(ch);
		}
		buildAC_automation();
		gets(str);
		printf("%d\n", query(str));
	}
	return ;
}
int main()
{
	ace();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值