HDU 3065 AC自动机

/****************************************************************************************************
    AC自动机,发现了一个不容易察觉的指针bug,不过不知道到底是怎么回事。。。
****************************************************************************************************/
#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 SC(t, s) static_cast<t>(s)
#define LEN(s) static_cast<int>( strlen((s)) )
#define SZ(s) static_cast<int>( (s).size() )

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

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;

template <typename T>
T sqa(const T &x)
{
	return x * x;
}
template <typename T>
T gcd(const T&a, const T&b)
{
	if (!a || !b)
	{
		return max(a, b);
	}
	T t;
	while (t = a % b)
	{
		a = b;
		b = t;
	}
	return b;
}
template <typename T>
T ext_gcd(T a, T b, T &x, T &y)
{
	if (!b)
	{
		x = 1;
		y = 0;
		return a;
	}
	T d = ext_gcd(b, a % b, x, y);
	T t = x;
	x = y;
	y = t - a / b * y;
	return d;
}

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 = 1004;
const int MAXK = 52;
const int MAXC = 128;
const int MAXL = 2000004;

int n, dp[MAXN];
char ch[MAXN][MAXK], str[MAXL];
struct Node
{
	Node *next[MAXC];
	Node *fail;
	int id;
}node[MAXN * MAXK], *const root = node;
int ncnt;

void initTrie()
{
	ncnt = 1;
	for (int i = 0; i < MAXC; ++i)
	{
		root->next[i] = NULL;
	}
	root->fail = NULL;
	root->id = -1;
	return ;
}
Node * createNode()
{
	Node *ptr = &node[ncnt++];
	for (int i = 0; i < MAXC; ++i)
	{
		ptr->next[i] = NULL;
	}
	ptr->fail = NULL;
	ptr->id = -1;
	return ptr;
}
void insert(char *ch, int _id)
{
	Node *ptr = root;
	int buf;
	while (*ch)
	{
		buf = *ch;
		if (NULL == ptr->next[buf])
		{
			ptr->next[buf] = createNode();
		}
		ptr = ptr->next[buf];
		++ch;
	}
	ptr->id = _id;
	return ;
}
void buildAC_automation()
{
	queue<Node *>Q;
	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];
						break ;
					}
				}
				if (NULL == ptr)
				{
					nxt->fail = root;
				}
				Q.push(nxt);
			}
			else
			{
				if (root == crt)
				{
					crt->next[i] = root;	//如果写成nxt = root;
				}
				else
				{
					crt->next[i] = crt->fail->next[i];	//nxt = crt->fail->next[i];	就错了?!求姐饰。。。
				}
			}
		}
	}
	return ;
}
void query(char *str)
{
	Node *ptr = root, *ptt;
	int buf;
	while (*str)
	{
		buf = *str;
		ptr = ptr->next[buf];
		for (ptt = ptr; ptt != NULL && ptt->id != -1; ptt = ptt->fail)
		{
			++dp[ptt->id];
		}
		++str;
	}
	return ;
}
void ace()
{
	while (scanf("%d%*c", &n) != EOF)
	{
		CLR(dp, 0);
		initTrie();
		for (int i = 0; i < n; ++i)
		{
			gets(ch[i]);
			insert(ch[i], i)
		}
		buildAC_automation();
		gets(str);
		query(str);
		for (int i = 0; i < n; ++i)
		{
			if (dp[i] != 0)
			{
				printf("%s: %d\n", ch[i], dp[i]);
			}
		}
	}
	return ;
}
int main()
{
	ace();
	return 0;
}

阅读更多
个人分类: HDU ACM-数据结构
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭