Tencent 2020 summer internship interview game client (compression algorithms)
Problem Description
Small Q want to give his friend a mysterious string, but he found that the string is too long,
so small Q developed a compression algorithm for string repeated portion is compressed,
for a continuous string S m identical strings will be compressed into m | S ,
for example, will be compressed into a string ABCABCABC [3 | ABC],
now small Q students received a string of small Q sent from, you can help him de compress it?
Enter a description
Input of the first line contains a string s, representing the compressed string.
Length S <= 1000;
S contains only uppercase letters, [,], |;
string length after decompression is not more than 100,000;
compression is not more than 10 layers recursive layers;
Output Description
Output a string representative of the decompressed string.
Example 1
Input
HG [3 | B [2 | CA]] F
output
HGBCACABCACABCACAF
described
HG [3 | B [2 | CA]] F-> HG [3 | BCACA] F-> HGBCACABCACABCACAF
注意:需考虑到中括号并列的情况,而非单纯括号嵌套
Ideas (string replacement)
- Flexible use of the string
[、|、]
location - Determining a first position closing parenthesis
- Determine the corresponding left parenthesis and separated number |
- Determining replacement string between the right partition and the number in brackets
- Determining the number of repetitions between the left bracket and the delimiter
- To replace the portion between the left and right parentheses string obtained to give the new S
- 2 is repeated until no brackets S, S is the output