~cpp
import unittest
class Slurpys:
def isSlump(self, string):
if((len(string) == 0) or\
(string[0] != 'D' and string[0] != 'E') or\
(string[1] != 'F') or\
(string[len(string) - 1] != 'G')):
return False
for i in range(2, len(string)):
if(string[i] == 'F'):
continue
elif(string[i] == 'G'):
return True
else:
return self.isSlump(string[i:])
return False
def isSlimp(self, string):
if(len(string) == 0 or string[0] != 'A'):
return False
if(len(string) == 2 and string == 'AH'):
return True
elif(len(string) > 2):
if(string[1] == 'B' and string[len(string) - 1] == 'C'):
return self.isSlimp(string[2:len(string)-1])
elif(string[len(string) - 1] == 'C'):
return self.isSlump(string[1:len(string)-1])
return False
def isSlurpy(self, string):
slimp = ''
slump = ''
if(len(string.split('C')) == 1):
slimp = 'AH'
slump = string.split('H')[1]
elif(len(string.split('C')) >= 2):
slimp = string.split('C')[0]
for i in range(len(string.split('C')) - 1):
slimp += 'C'
slump = string.split('C')[len(string.split('C')) - 1]
return self.isSlimp(slimp) and self.isSlump(slump)
class SlurpysTestCase(unittest.TestCase):
def testSlump(self):
slurpys = Slurpys()
self.assertEquals(True, slurpys.isSlump('DFG'))
self.assertEquals(True, slurpys.isSlump('EFG'))
self.assertEquals(True, slurpys.isSlump('DFFFFFG'))
self.assertEquals(True, slurpys.isSlump('DFDFDFDFG'))
self.assertEquals(True, slurpys.isSlump('DFEFFFFFG'))
def testNotSlump(self):
slurpys = Slurpys()
self.assertEquals(False, slurpys.isSlump('DFEFF'))
self.assertEquals(False, slurpys.isSlump('EFAHG'))
self.assertEquals(False, slurpys.isSlump('DEFG'))
self.assertEquals(False, slurpys.isSlump('DG'))
self.assertEquals(False, slurpys.isSlump('EFFFFDG'))
def testSlimp(self):
slurpys = Slurpys()
self.assertEquals(True, slurpys.isSlimp('AH'))
self.assertEquals(True, slurpys.isSlimp('ABAHC'))
self.assertEquals(True, slurpys.isSlimp('ABABAHCC'))
self.assertEquals(True, slurpys.isSlimp('ADFGC'))
self.assertEquals(True, slurpys.isSlimp('ADFFFFGC'))
self.assertEquals(True, slurpys.isSlimp('ABAEFGCC'))
self.assertEquals(True, slurpys.isSlimp('ADFDFGC'))
def testNotSlimp(self):
slurpys = Slurpys()
self.assertEquals(False, slurpys.isSlimp('ABC'))
self.assertEquals(False, slurpys.isSlimp('ABAH'))
self.assertEquals(False, slurpys.isSlimp('DFGC'))
self.assertEquals(False, slurpys.isSlimp('ABABAHC'))
self.assertEquals(False, slurpys.isSlimp('SLIMP'))
self.assertEquals(False, slurpys.isSlimp('ADGC'))
def testSlurpy(self):
slurpys = Slurpys()
self.assertEquals(True, slurpys.isSlurpy('AHDFG'))
self.assertEquals(True, slurpys.isSlurpy('ADFGCDFFFFFG'))
self.assertEquals(True, slurpys.isSlurpy('ABAEFGCCDFEFFFFFG'))
def testNotSlurpy(self):
slurpys = Slurpys()
self.assertEquals(False, slurpys.isSlurpy('AHDFGA'))
self.assertEquals(False, slurpys.isSlurpy('DFGAH'))
self.assertEquals(False, slurpys.isSlurpy('ABABCC'))
def showSlurpy(n, string):
print 'SLURPYS OUTPUT'
for i in range(n):
if(slurpys.isSlurpy(strings[i]) == True):
print 'YES'
else:
print 'NO'
print 'END OF OUTPUT'
if __name__=='__main__':
#unittest.main()
n = input()
strings = []
slurpys = Slurpys()
for i in range(n):
strings.append(raw_input())
showSlurpy(n, strings)
우선은 최대한 속도에 주력. 한데 완성 하고나서 리펙토링을 어찌해야할 지도 난감함. --재동