Check the String

시간 제한2초메모리 제한1024 MB

요약
copy, swap, roll, fuse 명령을 실행하는 스택 기계를 시뮬레이션하면서 CRASH 발생 여부를 판정하고 마지막 문자열이 s와 같은지 확인한다.
난이도

보통10점 중 6점

유형
시뮬레이션, 문자열, 스택, 구현
정답자
아직 제출이 없습니다

문제

Suppose we have a virtual machine that is described in the statement of problem B.

Given string ss and a program in the form of a list of nn commands for the given machine (each command is one from the list "copy", "swap", "roll", "fuse").

Initially, the virtual machine stack contains two strings and looks like "a b".

All commands from the list are sequentially executed on the virtual machine.

Determine if all the commands from the list were executed correctly, or if the execution of one of them resulted with the CRASH event. If all program commands were executed correctly --- additionally determine whether the string at the end of the machine's stack after the execution of all commands is equal to the ss string or not.

입력

The first line contains the string ss. It consists only of lowercase Latin letters "a" and "b" and has a length from 11 to 10510^5 characters.

The second line contains an integer nn (0≤n≤1050 \le n \le 10^5).

The next nn lines contain the program commands, one per line, ordered in the order of execution on the virtual machine. Each of the commands is one from the list "copy", "swap", "roll", "fuse".

출력

Print exactly one of the following messages:

  • CRASH --- if the CRASH event occurred during the execution of the program.
  • YES --- if after executing all the commands the string at the end of the machine's stack is equal to ss.
  • NO --- if after executing all the commands the string at the end of the stack is not equal to ss.

예제3

  1. 예제 1

    입력
    ababa
    9
    swap
    copy
    roll
    fuse
    copy
    fuse
    copy
    roll
    fuse
    
    예상 출력
    YES
    
  2. 예제 2

    입력
    a
    0
    
    예상 출력
    NO
    
  3. 예제 3

    입력
    aaba
    1
    roll
    
    예상 출력
    CRASH