String Game
Time limit1sMemory limit512 MB
Given strings W and S and N commands that each remove the leftmost or rightmost occurrence of W from S, report how many commands succeed and the final S.
- Level
Medium5 of 10
- Topics
- String, String matching, Simulation
- Solved
- No attempts yet
Problem
A string game is played as follows.
- A string W of lowercase English letters is given.
- A string S of lowercase English letters is given.
- The player who removes all of W from S wins.
Byeongseo wants to write a program that removes all of W from S.
His program repeats the following operation N times, running one of the commands below each time.
- L: Finds the first occurrence of W from the left end of S and removes that substring from S.
- R: Finds the first occurrence of W from the right end of S and removes that substring from S.
If W does not appear in S when a command runs, the command fails and nothing changes.
Following N commands, write a program that removes W from S.
Input
The first line and the second line contain the strings W and S, each with at least 1 letter. Both W and S consist of lowercase English letters.
W has at most 10 letters, and S has at most 300,000 letters.
The third line contains the number of commands N (1 to 30,000).
The next N lines each contain one command.
Output
On the first line, print the number of commands that succeeded. On the second line, print the string S after the program runs. The string S is never empty after the program runs.
On the third line, print You Lose! if W still appears in S after the program runs, or Perfect! if all of W was removed.