阿狸的打字机 NOI2011

 AC自动机是肯定的了,但关键是如何完成快速匹配,如果像普通的自动机那样在每个状态都通过fail转移累加的话肯定会超时的,所以需要一种新的方法,那就是维护fail逆向指针树的dfs序列,听上去挺麻烦的,先把所有fail指针逆向,这样就得到了一棵树(因为每个节点的出度都为1,所以逆向后每个节点入度为1,所以得到的是一棵树),而一棵树的先序dsf序列有一个很好的性质就是同任一棵子树的节点是连续排列的(前几天做的LCA的题也是维护dfs序列),先序dfs得到dfs序列,记录每棵子树的左界和右界,想想我们以前通过fail指针转移,其实就是在这树上从子节点向父节点转移,对于x串在y串中出现,必然是在y串某个前缀的后缀与x串相同,给每个当前访问的y串的节点权值+1,即该串的每个前缀权值都为1,如果x串在y串中出现,再回忆fail指针的定义,指向与该节点表示串后缀相等的且长度最大的串(或前缀)的节点,所以只要在y串的某个前缀的后缀等于x串,则沿着表示该前缀的节点的fail指针向上,必然能够到达表示x串的节点。只要在保证trie树中结点只有表示y串前缀的节点权值为1,其他节点权值为0,求x串在y串中出现的次数,把以x串为根节点的所以子节点权值加和就可以了,利用dfs序和树状数组就可以实现。

但只通过这个方法我们还不能得到高效的算法,因为每次都要初始化树状数组,而这题的实际操作只有俩种即1增加一个字符,2删除一个字符,所以我们可以在trie图中额外增加一个fa指针指向父节点,这样我们就可以得到高效的插入方法和匹配方法,而且不用每个串都初始化树状数组(实际上根本不用处理出每个串来)。复杂度为O((len+m)*log(len)),

这题我参考别人的代码还搞了2个小时,真心ORZ当时考试中AC此题的OIer,也感到了自己的蒟蒻。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;

const int MAXN(500015);
const int MAXM(10010);
const int MAXE(10010);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI(2000000000);
const int MOD(1000000007);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);

struct FENWICK_TREE
{
	int table[MAXN];
	int size;
	inline int lowbit(int sour){return sour&-sour;}
	void init(int tn)
	{
		memset(table+1, 0, sizeof(table[0])*tn);
		size = tn;
	}

	void add(int sour, int value)
	{
		for(int i = sour; i <= size; i += lowbit(i))
			table[i] += value;
	}
	int query(int sour)
	{
		int ret = 0;
		for(int i = sour; i >= 1; i -= lowbit(i))
			ret += table[i];
		return ret;
	}
};

FENWICK_TREE ft;

int left[MAXN], right[MAXN]; //子树左界,右界
int ind;

struct TREE  
{
	struct EDGE
	{
		int v, next;
	};
	int first[MAXN];
	EDGE edge[MAXN];
	int rear;
	void init()
	{
		memset(first, -1, sizeof(first));
		rear = 0;
	}
	void insert(int tu, int tv)
	{
		edge[rear].v = tv;
		edge[rear].next = first[tu];
		first[tu] = rear++;
	}
	void dfs(int u)
	{
		left[u] = ++ind;
		for(int i = first[u]; ~i; i = edge[i].next)
			dfs(edge[i].v);
		right[u] = ind;
	}
};

TREE tree;  //fail逆向指针树

vector<pair<int, int> > query[MAXN];  //询问
int words;
int hash[MAXN];                       //串到trie图节点的映射       
int ans[MAXN];                        //答案
bool vis[MAXN];                       //防止重复处理询问

struct AC
{
	int ch[MAXN][SIGMA_SIZE], f[MAXN],size;
	vector<int> rec[MAXN];                 //记录节点对应的串
	int fa[MAXN];
	inline int idx(char temp){return temp-'a';}
	void init()
	{
		memset(ch[0], 0, sizeof(ch[0]));
		f[0] = 0;
		size = 1;
	}

	void insert(char *S)
	{
		int u = 0, id;
		for(char *sp = S; *sp; ++sp)
			if(*sp == 'P')
			{
				hash[++words] = u;
				query[words].clear();
				rec[u].push_back(words);
			}
			else if(*sp == 'B')
				u = fa[u];
			else
			{
				id = idx(*sp);
				if(!ch[u][id])
				{
					memset(ch[size], 0, sizeof(ch[size]));
					fa[size] = u;
					rec[size].clear();
					ch[u][id] = size++;
				}
				u = ch[u][id];
			}
	}
	int que[MAXN];
	int front, back;
	void construct()
	{
		front = back = 0;
		int u, cur, id;
		for(int i = 0; i < SIGMA_SIZE; ++i)
			if(ch[0][i])
			{
				que[back++] = ch[0][i];
				f[ch[0][i]] = 0;
			}
		while(front < back)
		{
			cur = que[front++];
			for(int i = 0; i < SIGMA_SIZE; ++i)
			{
				u = ch[cur][i];
				if(u)
				{
					que[back++] = u;
					f[u] = ch[f[cur]][i];
				}
				else
					ch[cur][i] = ch[f[cur]][i];
			}
		}
	}

	void solve(char *S)
	{
		memset(vis, 0, sizeof(vis[0])*size);
		tree.init();
		for(int i = 1; i < size; ++i)
			tree.insert(f[i], i);
		ind = 0;
		tree.dfs(0);
		ft.init(size);
		int u = 0;
		for(char *sp = S; *sp; ++sp)
			if(*sp == 'P')
			{
				if(vis[u])
					continue;
				vis[u] = true;
				int ts1 = rec[u].size();
				for(int i = 0; i < ts1; ++i)
				{
					int y = rec[u][i];
					int ts2 = query[y].size();
					for(int j = 0; j < ts2; ++j)
					{
						int x = query[y][j].first, temp = query[y][j].second;
						ans[temp] = ft.query(right[hash[x]])-ft.query(left[hash[x]]-1);
					}
				}
			}
			else if(*sp == 'B')
			{
				ft.add(left[u], -1);
				u = fa[u];
			}
			else
			{
				u = ch[u][idx(*sp)];
				ft.add(left[u], 1);
			}
	}
};

AC ac;

char str[MAXN];

int main()
{
	while(~scanf("%s", str))
	{
		ac.init();
		words = 0;
		ac.insert(str);
		int n;
		scanf("%d", &n);
		int x, y;
		for(int i = 1; i <= n; ++i)
		{
			scanf("%d%d", &x, &y);
			query[y].push_back(make_pair(x, i));
		}
		ac.construct();
		ac.solve(str);
		for(int i = 1; i <= n; ++i)
			printf("%d\n", ans[i]);
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值