阿狸的打字机(AC自动机+dfs序 + 维护区间值)

链接:https://ac.nowcoder.com/acm/problem/17633

来源:牛客网

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

输入小写字母,打字机的一个凹槽中会加入这个字母(按P前凹槽中至少有一个字母)。
按一下印有'B'的按键,打字机凹槽中最后一个字母会消失。
按一下印有'P'的按键,打字机会在纸上打印出凹槽中现有的所有字母并换行,但凹槽中的字母不会消失(保证凹槽中至少有一个字母)。

例如,阿狸输入aPaPBbP,纸上被打印的字符如下: a aa ab 我们把纸上打印出来的字符串从1开始顺序编号,一直到n。打字机有一个非常有趣的功能,在打字机中暗藏一个带数字的小键盘,在小键盘上输入两个数(x,y)(其中1≤x,y≤n),打字机会显示第x个打印的字符串在第y个打印的字符串中出现了多少次。
阿狸发现了这个功能以后很兴奋,他想写个程序完成同样的功能,你能帮助他么?
(AC自动机上fail树dfs序建可持久化线段树?)
具体思路:
询问第x个字符串在第y个字符串中出现了多少次可以理解成 在字典树上第y个字符串所代表的路径中有多少节点的fail指针能指向在字典树上x字符串的末尾节点。
所以我们可以通过建立一颗fail树,每个节点连向 fail指针所指向这个节点的节点,然后我们通过对fail树跑一个dfs序,就能得到当前节点所控制的节点的编号。
然后我们跑每一个x字符串,每走一步这个x所维护的区间+1。然后询问的时候,判断当前字符串对应的询问字符串所代表的路径上的区间和就能求出来x字符串在第y个字符串出现了多少次。
注意跑的是单链,所以跑完当前的链之后应该取消赋值。
AC代码:
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 # define ll long long
  4 # define inf 0x3f3f3f3f
  5 const int maxn = 2e5+100;
  6 char str[maxn];
  7 int ch[maxn][30];
  8 int tot=0;
  9 int pre[maxn];
 10 int sto_str[maxn];
 11 int str_num=0;
 12 int sto[maxn];
 13 int tree[maxn];
 14 int dfsnum=0;
 15 int L[maxn],R[maxn];
 16 void add_trie()
 17 {
 18     int len=strlen(str);
 19     int p=0;
 20     for(int i=0; i<len; i++)
 21     {
 22         if(str[i]>='a'&&str[i]<='z')
 23         {
 24             int u=str[i]-'a';
 25             if(!ch[p][u])
 26                 ch[p][u]=++tot;
 27             pre[ch[p][u]]=p;
 28             p=ch[p][u];
 29         }
 30        else  if(str[i]=='B')// 当为B的时候,就回到上一个字符的位置
 31             p=pre[p];
 32        else if(str[i]=='P')
 33             sto_str[++str_num]=p;
 34     }
 35 }
 36 int fail[maxn];
 37 vector<int>fail_tree[maxn];
 38 void getfail()
 39 {
 40     queue<int>q;
 41     for(int i=0; i<=tot; i++)
 42         fail[i]=-1;
 43     q.push(0);
 44     while(!q.empty())
 45     {
 46         int top=q.front();
 47         q.pop();
 48         for(int i=0; i<26; i++)
 49         {
 50             int to=ch[top][i];
 51             if(!to)continue;
 52             q.push(to);
 53             int tmp=fail[top];
 54             while(tmp!=-1&&!ch[tmp][i])// 注意是while,,,调了半个小时
 55                 tmp=fail[tmp];
 56             fail[to] = (tmp==-1?0:ch[tmp][i]);
 57             fail_tree[fail[to]].push_back(to);
 58         }
 59     }
 60 }
 61 vector<pair<int,int> >sto_ans[maxn];
 62 void dfs_ord(int u)
 63 {
 64     L[u]=++dfsnum;
 65     for(int i=0; i<fail_tree[u].size(); i++)
 66     {
 67         dfs_ord(fail_tree[u][i]);
 68     }
 69     R[u]=dfsnum;
 70 }
 71 int lowbit(int t)
 72 {
 73     return t&(-t);
 74 }
 75 void add(int pos,int val)
 76 {
 77     while(pos<100000+10)
 78     {
 79         tree[pos]+=val;
 80         pos+=lowbit(pos);
 81     }
 82 }
 83 int ask(int pos)
 84 {
 85     int sum=0;
 86     while(pos)
 87     {
 88         sum+=tree[pos];
 89         pos-=lowbit(pos);
 90     }
 91     return sum;
 92 }
 93 void dfs_ans(int u)
 94 {
 95     for(int i=0; i<sto_ans[u].size(); i++)
 96     {
 97         int tmp=sto_ans[u][i].first;
 98         sto[sto_ans[u][i].second]=ask(R[tmp])-ask(L[tmp]-1);
 99     }
100     for(int i=0; i<26; i++)
101     {
102         int to=ch[u][i];
103         if(!to)continue;
104         add(L[to],1);
105         dfs_ans(to);
106         add(L[to],-1);
107     }
108 }
109 int main()
110 {
111     scanf("%s",str);
112     add_trie();
113     getfail();
114     int m,st,ed;
115     scanf("%d",&m);
116     for(int i=1; i<=m; i++)
117     {
118         scanf("%d %d",&st,&ed);
119         sto_ans[sto_str[ed]].push_back(make_pair(sto_str[st],i));
120     }
121     dfs_ord(0);
122     dfs_ans(0);
123     for(int i=1; i<=m; i++)
124     {
125         printf("%d\n",sto[i]);
126     }
127     return 0;
128 }

 

posted @ 2019-05-15 20:31  Let_Life_Stop  阅读(575)  评论(0编辑  收藏  举报