• BZOJ 2434([Noi2011]阿狸的打字机AC自动机Fail树)


    2434: [Noi2011]阿狸的打字机

    Time Limit: 10 Sec   Memory Limit: 128 MB
    Submit: 544   Solved: 300
    [ Submit][ Status][ Discuss]

    Description

     阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机。打字机上只有28个按键,分别印有26个小写英文字母和'B'、'P'两个字母。

    经阿狸研究发现,这个打字机是这样工作的:

    l 输入小写字母,打字机的一个凹槽中会加入这个字母(这个字母加在凹槽的最后)。

    l 按一下印有'B'的按键,打字机凹槽中最后一个字母会消失。

    l 按一下印有'P'的按键,打字机会在纸上打印出凹槽中现有的所有字母并换行,但凹槽中的字母不会消失。

    例如,阿狸输入aPaPBbP,纸上被打印的字符如下:

    a

    aa

    ab

    我们把纸上打印出来的字符串从1开始顺序编号,一直到n。打字机有一个非常有趣的功能,在打字机中暗藏一个带数字的小键盘,在小键盘上输入两个数(x,y)(其中1≤x,y≤n),打字机会显示第x个打印的字符串在第y个打印的字符串中出现了多少次。

    阿狸发现了这个功能以后很兴奋,他想写个程序完成同样的功能,你能帮助他么?

    Input

     输入的第一行包含一个字符串,按阿狸的输入顺序给出所有阿狸输入的字符。

    第二行包含一个整数m,表示询问个数。

    接下来m行描述所有由小键盘输入的询问。其中第i行包含两个整数x, y,表示第i个询问为(x, y)。

    Output

     输出m行,其中第i行包含一个整数,表示第i个询问的答案。

    Sample Input

    aPaPBbP

    3

    1 2

    1 3

    2 3

    Sample Output



    2

    1

    0

    HINT

     

     1<=N<=10^5


    1<=M<=10^5

    输入总长<=10^5

     

    Source


    总算知道Fail树了……(1年前各种弱渣看不懂……)
    Fail树是在AC自动机的前提下:
    把Fail指针反过来建一棵树。
    这样一个节点的子树的每个结点对应它的一个被匹配 //x在每一个子节点的串中出现过。
    这和原来x在每个结点中当过后缀不同。
    于是我很开心的 离线处理这个问题
    我们可以每时每刻维护 当前打字机中的串O(n)
    一遇到y,现在考虑x在y中出现次数,也就是Fail树中x的子节点中几个结点是y上的。
    我们发现这是一个平衡树求和问题:)事实上由于树静止,还可以用dfs序转成线段树区间求和(树状数组同……)
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<functional>
    #include<cmath>
    #include<cctype>
    using namespace std;
    #define For(i,n) for(int i=1;i<=n;i++)
    #define Rep(i,n) for(int i=0;i<n;i++)
    #define Fork(i,k,n) for(int i=k;i<=n;i++)
    #define ForD(i,n) for(int i=n;i;i--)
    #define Forp(x) for(int p=pre[x];p;p=next[p])
    #define RepD(i,n) for(int i=n;i>=0;i--)
    #define MAXN (100000+10)
    char s[MAXN];
    int n,m;
    struct comm
    {
    	int x,y,no;
    	friend bool operator<(comm a,comm b){return a.y<b.y;	}
    }ask[MAXN];
    int ans[MAXN];
    struct node
    {
    	node *ch[26],*fail,*fa;
    	int no;
    	node(){Rep(i,26) ch[i]=NULL;fail=NULL;fa=NULL;}
    	node(int _no,node *_fa):no(_no),fa(_fa){Rep(i,26) ch[i]=NULL;fail=NULL;}
    	node(int _no):no(_no){Rep(i,26) ch[i]=NULL;fail=NULL;fa=NULL;}
    }*root=new node(1);
    int tot=1;
    void ins(node *&x,char c)
    {
    	if (x->ch[c-'a']==NULL) x->ch[c-'a']=new node(++tot,x);
    	x=x->ch[c-'a'];
    }
    node *q[MAXN];
    int stail[MAXN];
    int stot=0;
    int edge[MAXN],pre[MAXN]={0},next[MAXN]={0},size=0;
    void addedge(int u,int v)
    {
    //	cout<<u<<' '<<v<<endl;
    	edge[++size]=v;
    	next[size]=pre[u];
    	pre[u]=size;
    }
    void getfail()
    {
    	int head=1,tail=1;q[1]=root;
    	while (head<=tail)
    	{
    		node *now=q[head];
    		Rep(i,26)
    			if (now->ch[i])
    			{
    				q[++tail]=now->ch[i];
    				if (now==root) {q[tail]->fail=root;addedge(root->no,q[tail]->no);continue;}
    				node *p=now->fail;
    				while (p!=root&&!p->ch[i]) p=p->fail;
    				if (p->ch[i]==NULL) q[tail]->fail=root,addedge(root->no,q[tail]->no);
    				else 
    				{
    					q[tail]->fail=p->ch[i];
    					addedge(p->ch[i]->no,q[tail]->no);
    				}
    			}
    		head++;
    	}
    }
    int tim=0,l[MAXN],r[MAXN];
    void dfs(int x,int fa)
    {
    //	cout<<"in:"<<x<<' ';
    	l[x]=++tim;
    	Forp(x)
    		if (edge[p]!=fa) dfs(edge[p],x);
    	r[x]=++tim;
    //	cout<<"out:"<<x<<' ';
    }
    int lowbit(int x){return x&(-x);}
    struct arr_tree
    {
    	int a[MAXN*2];
    	arr_tree(){memset(a,0,sizeof(a));}
    	void add(int x,int c)
    	{
    		for(;x<=tim;x+=x&(-x)) a[x]+=c;
    	}
    	int qur(int x)
    	{
    		int ans=0;
    		for(;x;x-=x&(-x)) ans+=a[x];
    		return ans;
    	}
    	
    }a;
    int main()
    {
    //	freopen("bzoj2434.in","r",stdin);
    //	freopen(".out","w",stdout);
    	scanf("%s",s+1);n=strlen(s+1);
    	root->fa=root->fail=root;
    	node *now=root;
    	For(i,n)
    	{
    		switch (s[i])
    		{
    			case 'P':stail[++stot]=tot;break;
    			case 'B':now=now->fa;break;
    			default:ins(now,s[i]);
    		}		
    	}
    	getfail();
    	dfs(root->no,0);	
    	scanf("%d",&m);
    	For(i,m) {scanf("%d%d",&ask[i].x,&ask[i].y);ask[i].no=i;}sort(ask+1,ask+1+m);ask[m+1].no=n+1;
    	now=root;
    	int now_ask=1,now_str=0;
    	For(i,n)
    	{
    		switch (s[i])
    		{
    			case 'P':
    				{
    					now_str++;
    					while (ask[now_ask].y==now_str)
    					{
    						int x=stail[ask[now_ask].x];
    						ans[ask[now_ask].no]=a.qur(r[x])-a.qur(l[x]-1);						
    						now_ask++;
    					}
    					break;
    				}
    			case 'B':a.add(l[now->no],-1);now=now->fa;break;
    			default:now=now->ch[s[i]-'a'];a.add(l[now->no],1);
    		}		
    	}	
    	For(i,m) printf("%d\n",ans[i]);
    	return 0;
    }


  • 相关阅读:
    P2216 [HAOI2007]理想的正方形(dp+单调队列优化)
    洛谷P1415 拆分数列(dp)
    2017 ACM-ICPC EC-Final ShangHai 东亚洲大陆-上海
    sql查询50题
    虚拟机安装
    Kick Start 2019 Round A Parcels
    Kick Start 2019 Round B Energy Stones
    【DP 好题】Kick Start 2019 Round C Catch Some
    【图论好题】ABC #142 Task F Pure
    【DP 好题】hihoCoder #1520 古老数字
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3091769.html
Copyright © 2020-2023  润新知