National Olympiad in Informatics, China, 2011
Ali likes to collect
all kinds of strange gadgets. Most recently, he's gotten his hands on an
old-fashioned typewriter. The typewriter contains B
and P
.
Through further examination, Ali learned that the typewriter worked like this:
- Hitting a lowercase letter, the typewriter will append this letter
onto a special groove. There will be at least one such letter before
hitting
P
. - Hitting the
B
key, the typewriter will remove the last letter added to the groove. - Hitting the
P
key, the typewriter will take all the letters currently on the groove and print them onto the paper, switching lines afterwards. However, the letters on the groove will not disappear nor change. There will be at least one letter on the groove for this operation.
For example, if Ali hits the keys aPaPBbP
, then the following
will be printed onto the paper:
a
aa
ab
We number the strings printed onto the paper from
Ali is very excited after discovering this feature. He wants to write a program to perform the exact same feature. Can you help him?
Input Specification
The first line contains a single string, describing all of the keys that
Ali presses on the typewriter.
The second line contains a single integer
For the following
Output Specification
Output
Sample Input
aPaPBbP
3
1 2
1 3
2 3
Sample Output
2
1
0
Constraints
The attributes of all the test cases are outlined below.
Test Case | Range of |
Range of |
Length of Strings | Keypresses (Line |
---|---|---|---|---|
/ | ||||
Individual lengths Total length |
||||
Total length | ||||
/ | ||||
Problem translated to English by .
Comments