#P2414. [NOI2011] 阿狸的打字机
[NOI2011] 阿狸的打字机
Description
Ali likes collecting all kinds of quirky things, and he recently found an old typewriter. The typewriter has only keys, labeled with the 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
Bkey is pressed once, the last letter in the slot disappears. - When the
Pkey 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 up to . The typewriter has a very interesting feature: it hides a numeric keypad. By entering two numbers (where ) on the keypad, the typewriter will display how many times the -th printed string appears in the -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 , the number of queries.
Each of the next lines describes one query entered via the keypad. The -th line contains two integers , representing the -th query .
Output Format
Output lines. The -th line should contain one integer, the answer to the -th query.
aPaPBbP
3
1 2
1 3
2 3
2
1
0
Hint
Constraints
For of the testdata, , , and the length of the first line .
| Test Points | scale | scale | string length | first line length |
|---|---|---|---|---|
| - | ||||
| single length , total length | ||||
| total length | ||||
| - |
Translated by ChatGPT 5
京公网安备 11011102002149号