This page is still under construction.

Parts of this page are still being built. What you see may change.

Slurpys

Interview

Time limit1sMemory limit128 MB

Summary
Given up to 10 short strings, decide whether each one is a Slurpy, meaning a Slimp followed by a Slump under the recursive grammar in the statement.
Level

Medium5 of 10

Topics
Recursion, String, Implementation, Backtracking
Solved
No attempts yet

Problem

Recognizing strings based on a set of restrictions is a common computational problem. A Slurpy is a string of characters that has certain properties. Your program reads strings of characters and outputs whether or not each one is a Slurpy.

A Slump is a character string with the following properties:

  1. Its first character is either a D or an E.
  2. That first character is followed by a string of one or more Fs.
  3. The string of one or more Fs is followed by either a Slump or a G. The Slump or G that follows the Fs ends the Slump. For example, DFFEFFFG is a Slump: it starts with a D, followed by two Fs, and is ended by the Slump EFFFG.
  4. Nothing else is a Slump.

A Slimp is a character string with the following properties:

  1. Its first character is an A.
  2. If it is a two-character Slimp, then its second and last character is an H.
  3. If it is not a two-character Slimp, then it has one of these two forms:
    • A followed by B followed by a Slimp followed by a C.
    • A followed by a Slump (see above) followed by a C.
  4. Nothing else is a Slimp.

A Slurpy is a character string that consists of a Slimp followed by a Slump.

Examples

  • Slumps: DFG, EFG, DFFFFFG, DFDFDFDFG, DFEFFFFFG
  • Not Slumps: DFEFF, EFAHG, DEFG, DG, EFFFFDG
  • Slimps: AH, ABAHC, ABABAHCC, ADFGC, ADFFFFGC, ABAEFGCC, ADFDFGC
  • Not Slimps: ABC, ABAH, DFGC, ABABAHC, SLIMP, ADGC
  • Slurpys: AHDFG, ADFGCDFFFFFG, ABAEFGCCDFEFFFFFG
  • Not Slurpys: AHDFGA, DFGAH, ABABCC

Input

The first line contains an integer NN (1≤N≤101 \le N \le 10), the number of strings. Each of the next NN lines contains a string of 1 to 60 alphabetic characters.

Output

For each of the NN input strings, output YES or NO on its own line, indicating whether the corresponding string is a Slurpy.

Examples2

  1. Example 1

    Input
    2
    AHDFG
    DFGAH
    
    Expected output
    YES
    NO
    
  2. Example 2

    Input
    6
    AHDFG
    ADFGCDFFFFFG
    ABAEFGCCDFEFFFFFG
    AHDFGA
    DFGAH
    ABABCC
    
    Expected output
    YES
    YES
    YES
    NO
    NO
    NO