RLE Replacement
Time limit2sMemory limit128 MB
Replace the first occurrence of RLE string B inside RLE string A with RLE string C and print the result in RLE form.
- Level
Medium6 of 10
- Topics
- String matching, Two pointers, Implementation
- Solved
- No attempts yet
Problem
Source code in one programming language is written with uppercase English letters only, and the same letter often repeats many times in a row. Very large source files in that language are stored compressed by run length encoding.
Run length encoding (RLE) rewrites every maximal block of identical letters as a pair of that letter and the length of the block. For example, the string RRRRLEEE becomes R4L1E3 under RLE.
Three strings , , and are given in RLE form. Write a program that replaces the first occurrence of as a substring of with and prints the resulting string in RLE form. If does not occur in , print in RLE form unchanged.
Input
The input consists of three lines.
A
B
C
The three lines hold the RLE encodings of , , and , in that order. Each line has this format:
c1 l1 c2 l2 ... cn ln $
Each () is an uppercase English letter (A through Z), and each (, ) is an integer, the length of the repetition of . The number of pairs satisfies . The letters and the integers are separated by a single space, and the terminal symbol $ ends the line. For every with , holds.
Output
If occurs in , replace the first occurrence of with ; otherwise keep as it is. Print the result in RLE form on one line, in this format:
c1 l1 c2 l2 ... cm lm $
The output must satisfy for and for .