【AC自动机】 HDOJ 5069 Harry And Biological Teacher

利用AC自动机的fail指针建树,然后就可以了。。。


#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 200005
#define maxm 200005
#define eps 1e-10
#define mod 1000000007
#define INF 1e17
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
//#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
// head
//4897

struct AC
{
	vector<int> vec[maxn];
	int next[maxn][4];
	int fail[maxn];
	int end[maxn];
	int du[maxn];
	int hash[300];
	char s[maxn];
	queue<int> q;
	int root, now, top;

	int newnode(void)
	{
		vec[top].clear();
		du[top] = 0;
		end[top] = fail[top] = -1;
		for(int i = 0; i < 4; i++) next[top][i] = -1;
		return top++;
	}

	void init(void)
	{
		top = 0;
		root = newnode();
		hash['A'] = 0;
		hash['C'] = 1;
		hash['G'] = 2;
		hash['T'] = 3;
	}

	int insert(int x)
	{
		now = root;
		scanf("%s", s);
		int len = strlen(s), tt = 0;
		for(int i = 0; i < len; i++) {
			int t = hash[(int)s[i]];
			if(next[now][t] == -1) next[now][t] = newnode();
			now = next[now][t];
			du[now] = ++tt;
			vec[now].push_back(x);
		}
		if(end[now] == -1) {
			end[now] = x;
			return x;
		}
		else return end[now];
	}

	void build(void)
	{
		now = root;
		for(int i = 0; i < 4; i++)
			if(next[now][i] == -1) next[now][i] = root;
			else {
				fail[next[now][i]] = root;
				q.push(next[now][i]);
			}
		while(!q.empty()) {
			now = q.front(), q.pop();
			for(int i = 0; i < 4; i++)
				if(next[now][i] == -1) next[now][i] = next[fail[now]][i];
				else {
					fail[next[now][i]] = next[fail[now]][i];
					q.push(next[now][i]);
				}
		}
	}
}ac;

struct Edge
{
	int v;
	Edge *next;
}pool[maxm], *edges, *H[maxn];

int mpp[maxn];
set<int> s[maxn];
vector<pair<int, int> > vec[maxn];
int res[maxn];
char str[maxn];
int n, m;

void addedges(int u, int v)
{
	edges->v = v;
	edges->next = H[u];
	H[u] = edges++;
}

void init(void)
{
	edges = pool;
	memset(H, 0, sizeof H);
	for(int i = 0; i < maxn; i++) s[i].clear();
	for(int i = 0; i < maxn; i++) vec[i].clear();
}

void read(void)
{
	int a, b;
	ac.init();
	for(int i = 1; i <= n; i++) mpp[i] = ac.insert(i);
	ac.build();
	for(int i = 1; i <= m; i++) {
		scanf("%d%d", &a, &b);
		vec[mpp[a]].push_back(mp(mpp[b], i));
	}
}

void dfs(int now, int d)
{ 
	int t = ac.vec[now].size();
	for(int i = 0; i < t; i++) s[ac.vec[now][i]].insert(d);
	if(ac.end[now] != -1) {
		int tt = ac.end[now], size = vec[tt].size();
		for(int i = 0; i < size; i++) {
			if(s[vec[tt][i].first].empty()) res[vec[tt][i].second] = 0;
			else res[vec[tt][i].second] = *(--s[vec[tt][i].first].end());
		}
	}
	for(Edge *e = H[now]; e; e = e->next) dfs(e->v, ac.du[e->v]);
	for(int i = 0; i < t; i++) s[ac.vec[now][i]].erase(d);
}

void work(void)
{
	for(int i = 1; i < ac.top; i++) addedges(ac.fail[i], i);
	dfs(ac.root, 0);
	for(int i = 1; i <= m; i++) {
		printf("%d\n", res[i]);
	}
}

int main(void)
{
	while(scanf("%d%d", &n, &m)!=EOF) {
		init();
		read();
		work();
	}

	return 0;
}



你可能感兴趣的:(HDU)