题目描述
阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机。打字机上只有28个按键,分别印有26个小写英文字母和‘B‘、‘P‘两个字母。
经阿狸研究发现,这个打字机是这样工作的:
l 输入小写字母,打字机的一个凹槽中会加入这个字母(这个字母加在凹槽的最后)。
l 按一下印有‘B‘的按键,打字机凹槽中最后一个字母会消失。
l 按一下印有‘P‘的按键,打字机会在纸上打印出凹槽中现有的所有字母并换行,但凹槽中的字母不会消失。
例如,阿狸输入aPaPBbP,纸上被打印的字符如下:
a
aa
ab
我们把纸上打印出来的字符串从1开始顺序编号,一直到n。打字机有一个非常有趣的功能,在打字机中暗藏一个带数字的小键盘,在小键盘上输入两个数(x,y)(其中1≤x,y≤n),打字机会显示第x个打印的字符串在第y个打印的字符串中出现了多少次。
阿狸发现了这个功能以后很兴奋,他想写个程序完成同样的功能,你能帮助他么?
输入
输入的第一行包含一个字符串,按阿狸的输入顺序给出所有阿狸输入的字符。
第二行包含一个整数m,表示询问个数。
接下来m行描述所有由小键盘输入的询问。其中第i行包含两个整数x, y,表示第i个询问为(x, y)。
输出
输出m行,其中第i行包含一个整数,表示第i个询问的答案。
样例输入
aPaPBbP
3
1 2
1 3
2 3
样例输出
2
1
0
题解
AC自动机
题目中打字的方法其实就是在建立Trie树,根据这个可以建出一棵Trie树。
首先必须要知道,x串在y串中出现的次数,就是y串中可以通过fail链到达x串的节点个数,
也就是fail树中,x串的子树中y串中节点的个数。
接下来的问题比较复杂。
怎样求fail树中x串的子树中y串中节点的个数?也即y串中节点有几个在x串中。
这需要dfs序,l与r之间是对应子树,可以用树状数组进行单点修改,区间查询。
在AC自动机中遍历一遍,遍历到y时,把l[y]加1,退出时把r[y]都减1。
那么可以发现:这就是给定的打字方式!
‘a‘~‘z‘:添加字符 ‘a‘~‘z‘:数组加1
‘B‘:删除字符 => ‘B‘:数组减1
‘P‘:打印字符 ‘P‘:处理询问
于是再次对原字符串进行处理,即可
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <vector> 5 #define N 1000001 6 using namespace std; 7 queue<int> q; 8 int nt[N][26] , fa[N] , fail[N] , cnt[N] , tot = 1 , pos[N] , p , c , head[N] , to[N] , nextn[N] , l[N] , r[N] , f[N << 1] , k , ans[N]; 9 char str[N]; 10 vector<int> son[N]; 11 void add(int x , int y) 12 { 13 to[++c] = y; 14 nextn[c] = head[x]; 15 head[x] = c; 16 } 17 void build() 18 { 19 int u , t , i; 20 q.push(1); 21 while(!q.empty()) 22 { 23 u = q.front(); 24 q.pop(); 25 for(i = 0 ; i < 26 ; i ++ ) 26 { 27 if(nt[u][i]) 28 { 29 q.push(nt[u][i]); 30 t = fail[u]; 31 while(t && !nt[t][i]) 32 t = fail[t]; 33 if(t) fail[nt[u][i]] = nt[t][i]; 34 else fail[nt[u][i]] = 1; 35 } 36 } 37 } 38 } 39 void dfs(int x) 40 { 41 l[x] = ++k; 42 int i; 43 for(i = 0 ; i < (int)son[x].size() ; i ++ ) 44 dfs(son[x][i]); 45 r[x] = ++k; 46 } 47 void update(int x , int a) 48 { 49 int i; 50 for(i = x ; i <= k ; i += i & -i) 51 f[i] += a; 52 } 53 int query(int x) 54 { 55 int i , ans = 0; 56 for(i = x ; i ; i -= i & -i) 57 ans += f[i]; 58 return ans; 59 } 60 void solve() 61 { 62 int now = 1 , id = 0 , i , j , len = strlen(str); 63 update(l[1] , 1); 64 for(i = 0 ; i < len ; i ++ ) 65 { 66 if(str[i] == ‘P‘) 67 for(j = head[++id] ; j ; j = nextn[j]) 68 ans[j] += query(r[pos[to[j]]]) - query(l[pos[to[j]]] - 1); 69 else if(str[i] == ‘B‘) 70 { 71 update(r[now] , -1); 72 now = fa[now]; 73 } 74 else 75 { 76 now = nt[now][str[i] - ‘a‘]; 77 update(l[now] , 1); 78 } 79 } 80 } 81 int main() 82 { 83 int i , t = 1 , len , x , y , m; 84 scanf("%s" , str); 85 len = strlen(str); 86 for(i = 0 ; i < len ; i ++ ) 87 { 88 if(str[i] == ‘P‘) 89 { 90 cnt[t] ++ ; 91 pos[++p] = t; 92 } 93 else if(str[i] == ‘B‘) 94 t = fa[t]; 95 else 96 { 97 if(!nt[t][str[i] - ‘a‘]) 98 nt[t][str[i] - ‘a‘] = ++tot; 99 fa[nt[t][str[i] - ‘a‘]] = t; 100 t = nt[t][str[i] - ‘a‘]; 101 } 102 } 103 build(); 104 for(i = 1 ; i <= tot ; i ++ ) 105 son[fail[i]].push_back(i); 106 dfs(1); 107 scanf("%d" , &m); 108 for(i = 1 ; i <= m ; i ++ ) 109 { 110 scanf("%d%d" , &x , &y); 111 add(y , x); 112 } 113 solve(); 114 for(i = 1 ; i <= m ; i ++ ) 115 printf("%d\n" , ans[i]); 116 return 0; 117 }
得到答案。
原文:http://www.cnblogs.com/GXZlegend/p/6265087.html