【AC自动机】 BZOJ 2434 [Noi2011]阿狸的打字机

经典的利用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

int mpp[maxn];
struct AC
{
	int next[maxn][26];
	int ch[maxn][26];
	int fail[maxn];
	int end[maxn];
	int fa[maxn];
	char s[maxn];
	queue<int> q;
	int root, now, top;

	int newnode(int f)
	{
		fa[top] = f;
		fail[top] = end[top] = -1;
		for(int i = 0; i < 26; i++) next[top][i] = ch[top][i] = -1;
		return top++;
	}

	void init(void)
	{
		top = 0;
		root = newnode(top);
		fa[root] = root;
	}

	void insert(void)
	{
		scanf("%s", s);
		int len = strlen(s), id = 0, t;
		now = root;
		for(int i = 0; i < len; i++) {
			t = s[i] - 'a';
			if(s[i] == 'B') now = fa[now];
			else if(s[i] == 'P') {
				if(end[now] == -1) end[now] = ++id, mpp[id] = id;
				else mpp[++id] = end[now];
			}
			else {
				if(next[now][t] == -1) ch[now][t] = next[now][t] = newnode(now);
				now = next[now][t];
			}
		}
	}

	void build(void)
	{
		now = root;
		for(int i = 0; i < 26; 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 < 26; 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], *H[maxn], *edges;

vector<pair<int, int> > vec[maxn];
int tree[maxn], ans[maxn];
int in[maxn], out[maxn];
int idx[maxn];
int n, m, dfs_clock;

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

void add(int x, int c)
{
	for(int i = x; i < maxn; i += lowbit(i)) tree[i] += c;
}

int sum(int x)
{
	int res = 0;
	for(int i = x; i > 0; i -= lowbit(i)) res += tree[i];
	return res;
}

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

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

void DFS(int u)
{
	in[u] = ++dfs_clock;
	for(Edge *e = H[u]; e; e = e->next) DFS(e->v);
	out[u] = dfs_clock;
}

void dfs(int u)
{
	add(in[u], 1);
	for(int i = 0; i < 26; i++) if(ac.ch[u][i] != -1) dfs(ac.ch[u][i]);
	if(ac.end[u] != -1) {
		int t = ac.end[u], size = vec[t].size();
		for(int i = 0; i < size; i++) {
			if(vec[t][i].first == 0) continue;
			ans[vec[t][i].second] = sum(out[idx[vec[t][i].first]]) - sum(in[idx[vec[t][i].first]] - 1);
		}
	}
	add(in[u], -1);
}

void work(void)
{
	for(int i = 1; i < ac.top; i++) addedges(ac.fail[i], i);
	for(int i = 1; i < ac.top; i++) if(ac.end[i] != -1) idx[ac.end[i]] = i;
	DFS(ac.root);
	dfs(ac.root);
	for(int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}

int main(void)
{
	init();
	read();
	work();

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值