bzoj2434: [Noi2011] Ali's typewriter

Source: Internet
Author: User

Description

Ali like to collect all kinds of strange things, he recently Amoy to an old-fashioned typewriter. There are only 28 keys on the typewriter, with 26 lowercase English letters and ' B ' and ' P ' two letters printed respectively.

The ALI study found that the typewriter worked like this:

L Enter lowercase letters and the letter is added to a groove in the typewriter (the letter is added to the end of the groove).

L PRESS the ' B ' button and the last letter in the typewriter groove disappears.

L PRESS the ' P ' button and the typewriter will print all the existing letters in the groove and wrap them on the paper, but the letters in the groove will not disappear.

For example, Ali input APAPBBP, the characters printed on the paper are as follows:

A

Aa

Ab

We numbered the printed string on the paper from 1 onwards to N. Typewriter has a very interesting function, in the typewriter hidden a small keyboard with a number, on the keypad input two number (x, y) (where 1≤x,y≤n), the typewriter will show the first x printed string in the first number of printed strings in the occurrence of how many times.

Ali found this feature was very excited, he wanted to write a program to complete the same function, can you help him?

Input

The first line of input contains a string that gives all the characters of the beaver input in the order of Ali input.

The second line contains an integer m, which indicates the number of queries.

The next M-line describes all the queries entered by the keypad. Where line I contains two integers x, y, which indicates that I inquired for (x, y).

Output

Output m line, where line I contains an integer representing the answer to the I query.

Sample InputAPAPBBP

3

1 2

1 3

2 3

Sample Output2

1

0HINT

1<=n<=10^5


1<=m<=10^5
Input total length <=10^5

Source

Trie

Exercises

http://blog.csdn.net/thy_asdf/article/details/47069707

Code

1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6#include <vector>7 #defineLowbit (x) ((x) & (-(x)))8 using namespacestd;9 Charch;Ten BOOLOK; One voidReadint&x) { A      for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; -      for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); -     if(OK) x=-x; the } - Const intmaxn=100005; - CharS[MAXN]; - intLEN,Q,X,Y,IDX,POS[MAXN],NUM[MAXN],ANS[MAXN]; +typedef pair<int,int>PII; -Vector<pii>QUERYS[MAXN]; + structbit{ A     intN,VAL[MAXN]; at     voidInitintx) {n=x;} -     voidModifyintXintV) { for(; x<=n;x+=lowbit (x)) val[x]+=v;} -     intQueryintx) { -         intres=0; -          for(; x;x-=lowbit (x)) res+=Val[x]; -         returnRes; in     } -     intQueryintLintR) {returnQuery (R)-query (l1);} to }bit; + structgraph{ -     intTOT,NOW[MAXN],SON[MAXN],PRE[MAXN],IDX,DFN[MAXN],LAST[MAXN]; the     voidPutintAintb) {pre[++tot]=now[a],now[a]=tot,son[tot]=b;} *     voidDfsintu) { $dfn[u]=++idx;Panax Notoginseng          for(intP=now[u],v=son[p];p; p=pre[p],v=Son[p]) Dfs (v); -last[u]=idx; the     } +     voidInit () {DFS (0), Bit.init (IDX);} A     voidChangeintUintv) {bit.modify (dfn[u],v);} the     intQueryintu) {returnbit.query (Dfn[u],last[u]);} + }g; - structtrie{ $     inttot,son[maxn][ -],FAI[MAXN],FA[MAXN]; $     intHEAD,TAIL,QUE[MAXN]; -     voidBuild_fai () { -Head=0, tail=1, que[1]=0, fai[0]=-1; the          while(head<tail) { -             intu=que[++Head];Wuyi              for(intCh=0,v=son[u][ch],t;ch< -; V=son[u][++ch])if(v) { theque[++tail]=v; -                  for(t=fai[u];t>=0&&!son[t][ch];t=fai[t]); Wu                 if(t>=0) Fai[v]=son[t][ch];Elsefai[v]=0; -             } About         } $     } -     voidinit () { -         intp=0; -          for(intI=1; i<=len;i++) A             if(s[i]=='P') pos[p]=++idx,num[idx]=p; +             Else if(s[i]=='B') p=Fa[p]; the             Else{ -                 if(!son[p][s[i]-'a']) son[p][s[i]-'a']=++tot,fa[tot]=p; $p=son[p][s[i]-'a']; the             } the Build_fai (); the          for(intI=1; i<=tot;i++) G.put (fai[i],i); the g.init (); -     } in     voidsolve () { the         intp=0; G.change (P,1); the          for(intI=1; i<=len;i++) About             if(s[i]=='P'){ the                 inty=Pos[p]; the                  for(unsignedintI=0; I<querys[y].size (); i++) ans[querys[y][i].second]=g.query (Num[querys[y][i].first]); the                  +             } -             Else if(s[i]=='B') G.change (p,-1), p=Fa[p]; the             Elsep=son[p][s[i]-'a'],g.change (P,1);Bayi     } the }t; the intMain () { -scanf"%s", s+1), Len=strlen (s+1); - read (q); the      for(intI=1; i<=q;i++read (x), read (y), Querys[y].push_back (Make_pair (x,i)); the t.init (), T.solve (); the      for(intI=1; i<=q;i++) printf ("%d\n", Ans[i]); the     return 0; -}

bzoj2434: [Noi2011] Ali's typewriter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.