Slurpys
InterviewTime limit1sMemory limit128 MB
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:
- Its first character is either a
Dor anE. - That first character is followed by a string of one or more
Fs. - The string of one or more
Fs is followed by either a Slump or aG. The Slump orGthat follows theFs ends the Slump. For example,DFFEFFFGis a Slump: it starts with aD, followed by twoFs, and is ended by the SlumpEFFFG. - Nothing else is a Slump.
A Slimp is a character string with the following properties:
- Its first character is an
A. - If it is a two-character Slimp, then its second and last character is an
H. - If it is not a two-character Slimp, then it has one of these two forms:
Afollowed byBfollowed by a Slimp followed by aC.Afollowed by a Slump (see above) followed by aC.
- 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 (), the number of strings. Each of the next lines contains a string of 1 to 60 alphabetic characters.
Output
For each of the input strings, output YES or NO on its own line, indicating whether the corresponding string is a Slurpy.