Under construction

Still being built. Things may change

String Game

Time limit1sMemory limit512 MB

Summary
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.

  1. A string W of lowercase English letters is given.
  2. A string S of lowercase English letters is given.
  3. 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.

Examples3

  1. Example 1

    Input
    zoac
    aliczoacethrozoacughthelookingzoacglaszoacs
    4
    L
    R
    L
    L
    
    Expected output
    4
    alicethroughthelookingglass
    Perfect!
    
  2. Example 2

    Input
    poke
    superaquatpoppokeokpokeekeornado
    6
    L
    L
    L
    R
    R
    R
    
    Expected output
    4
    superaquatornado
    Perfect!
    
  3. Example 3

    Input
    erica
    irederiericacavelvetu
    1
    L
    
    Expected output
    1
    iredericavelvetu
    You Lose!