Bzoj 2434: [Noi2011] Ali Typewriter (AC automaton + DFS sequence + tree array)

Source: Internet
Author: User

A string a appears in B, then A is the suffix of some prefixes of B, so make an AC automaton, reverse the build by fail, and then query (x, y) is how much of Y's subtree is the x prefix. Offline, to the AC automaton Dfs again, with DFS Order + Tree array maintenance, DFS to the query point to answer the inquiry. Time Complexity O (| ACAM|+QLOGQ)

-------------------------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cctype>using namespace std; #define CHK (c) (c <= ' z ' && C >= ' a ') | | (c = = ' P ') | | (c = = ' B '))#define C (c) (C-' a ')const int MAXN = 100009;const int c = n;inline int Read () {char C = getchar ();For (;!isdigit (c); c = GetChar ());int ret = 0;For (; IsDigit (c); c = GetChar ())ret = ret * + C-' 0 ';return ret;}struct Node {Node *ch[c], *fail, *par;int V, D;Node (): V (0) {memset (CH, 0, sizeof ch);fail = par = 0;}} POOL[MAXN], *v[maxn], *pt = Pool, *rt;struct Q {int d, x, y;inline void Read (int _d) {d = _d;x = read ();y = read ();}BOOL Operator < (const Q &o) Const {return y < o.y;}} Q[MAXN];int N, DFSN, qn;int L[MAXN], R[MAXN], QL[MAXN], QR[MAXN], ANS[MAXN];void Init () {int cnt = DFSN = n = 0;pt->d = n++;Rt = pt++;node* t = Rt;char C = getchar ();For (;!chk (c); c = GetChar ());For (; chk (c); c = GetChar ()) {if (c = = ' P ') {v[t->v = ++cnt] = t;} else if (c = = ' B ') {t = t->par;} else {if (!t->ch[c (C)]) {pt->par = t;pt->d = n++;t->ch[c (C)] = pt++;}t = t->ch[c (C)];}}scanf ("%d", &qn);for (int i = 0; i < qn; i++)Q[i]. Read (i);sort (q, q + qn);memset (QL, 0, sizeof (int) * (CNT + 1));memset (QR,-1, sizeof (int) * (CNT + 1));for (int i = 0; i < qn; i++) {if (!i | | q[i-1].y! = Q[I].Y)QL[Q[I].Y] = i;if (i + 1 = = QN | | q[i + 1].Y! = q[i].y)QR[Q[I].Y] = i;}}struct Edge {int to;edge* Next;} E[MAXN << 1], *pt = E, *HEAD[MAXN];inline void Addedge (int u, int v) {pt->to = v;pt->next = Head[u];Head[u] = pt++;}queue<node*> que;void Buildfail () {Que.push (Rt);While (!que.empty ()) {node* t = Que.front (); Que.pop ();if (t->fail)Addedge (T->fail->d, t->d);for (int i = 0; i < C; i++) if (T->ch[i]) {node* f = t->fail;While (f &&!f->ch[i])f = f->fail;t->ch[i]->fail = f? F->ch[i]: Rt;Que.push (T->ch[i]);}}}struct BIT {int B[MAXN];BIT () {memset (b, 0, sizeof B);}inline void Add (int x, int v) {For (; x <= n; x + = x & x)B[x] + = V;}inline int Sum (int x) {int ret = 0;For (; x; x-= x &-X)ret + = b[x];return ret;}inline int Query (int l, int r) {return sum (r)-sum (L-1);}} Bit;void DFS (int x) {l[x] = ++DFSN;For (edge* e = head[x]; e; e = e->next) DFS (e->to);r[x] = DFSN;} void Dfsac (node* t) {Bit.add (L[t->d], 1);if (t->v) {for (int i = ql[t->v]; i <= qr[t->v]; i++)ANS[Q[I].D] = Bit.query (L[v[q[i].x]->d], r[v[q[i].x]->d]);}for (int i = 0; i < C; i++)if (T->ch[i]) Dfsac (T->ch[i]);Bit.add (L[t->d],-1);}int main () {Init ();Buildfail ();DFS (0);Dfsac (Rt);for (int i = 0; i < qn; i++)printf ("%d\n", Ans[i]);return 0;}

-------------------------------------------------------------------------------------------

2434: [Noi2011] Ali's typewriter time limit: ten Sec Memory Limit: + MB
Submit: 1706 Solved: 974
[Submit] [Status] [Discuss] 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

Bzoj 2434: [Noi2011] Ali Typewriter (AC automaton + DFS sequence + tree array)

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.