#P2414. [NOI2011] 阿狸的打字机

    ID: 1414 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>字符串树形结构2011NOI 系列深度优先搜索,DFSAC 自动机

[NOI2011] 阿狸的打字机

Description

Ali likes collecting all kinds of quirky things, and he recently found an old typewriter. The typewriter has only 2828 keys, labeled with the 2626 lowercase English letters and the two letters B and P. After some study, Ali found that the typewriter works as follows:

  • When a lowercase letter is entered, the letter is appended to a slot in the typewriter (this letter is added to the end of the slot).
  • When the B key is pressed once, the last letter in the slot disappears.
  • When the P key is pressed once, the typewriter prints all the current letters in the slot on the paper and starts a new line, but the letters in the slot remain.

For example, if Ali inputs aPaPBbP, the characters printed on the paper are:

a
aa
ab

We number the strings printed on the paper starting from 11 up to nn. The typewriter has a very interesting feature: it hides a numeric keypad. By entering two numbers (x,y)(x, y) (where 1x,yn1 \leq x, y \leq n) on the keypad, the typewriter will display how many times the xx-th printed string appears in the yy-th printed string.

After discovering this feature, Ali became excited and wanted to write a program to achieve the same function. Can you help him?

Input Format

The first line contains a string that lists all characters Ali inputs in order.

The second line contains an integer mm, the number of queries.

Each of the next mm lines describes one query entered via the keypad. The ii-th line contains two integers x,yx, y, representing the ii-th query (x,y)(x, y).

Output Format

Output mm lines. The ii-th line should contain one integer, the answer to the ii-th query.

aPaPBbP
3
1 2
1 3
2 3
2
1
0

Hint

Constraints

For 100%100\% of the testdata, 1n1051 \leq n \leq 10^5, 1m1051 \leq m \leq 10^5, and the length of the first line 105\leq 10^5.

Test Points nn scale mm scale string length first line length
1,21,2 1n1001 \leq n \leq 100 1m1031 \leq m \leq 10^3 - 100\leq 100
3,43,4 1n1031 \leq n \leq 10^3 1m1041 \leq m \leq 10^4 single length 103\leq 10^3, total length 105\leq 10^5 105\leq 10^5
575 \sim 7 1n1041 \leq n \leq 10^4 1m1051 \leq m \leq 10^5 total length 105\leq 10^5
8108 \sim 10 1n1051 \leq n \leq 10^5 -

Translated by ChatGPT 5