Find and Replace
Time limit2sMemory limit1024 MB
Starting from "a", apply letter-to-string replacements, then print the substring from position l to position r of the final string.
- Level
Medium6 of 10
- Topics
- Recursion, Dynamic programming, String
- Solved
- No attempts yet
Problem
Bessie is using miV, the newest text-editing software. Its find-and-replace feature lets her find every occurrence of a lowercase English letter and replace each one with a nonempty string of lowercase letters. For example, given the string "ball", if Bessie picks to be 'l' and to be "na", the string becomes "banana".
Bessie starts with the string "a" and applies a number of these operations, which produces a final string . Since can be very long, for given and with , she wants to know the substring from the -th to the -th character, inclusive.
The sum of over all operations is at most , and .
Input
The first line contains , , and the number of operations. Each of the following lines describes one operation and contains and . All characters are lowercase English letters from 'a' to 'z'.
Output
Print the string on a single line.
Hint
The string changes as follows:
a ab bcb bdeb bbbdebbb