Skip to content

Instantly share code, notes, and snippets.

@liu246542
Last active March 25, 2019 09:20
Show Gist options
  • Save liu246542/c44dcda3a2681612018161c0deb9d53c to your computer and use it in GitHub Desktop.
Save liu246542/c44dcda3a2681612018161c0deb9d53c to your computer and use it in GitHub Desktop.
第二题的代码参考
import re
import sys
def findChar(string):
upChar = re.compile('[A-Z]')
lowChar = re.compile('[a-z]')
ABCStr = ''.join(sorted(upChar.findall(string)))
abcStr = ''.join(sorted(lowChar.findall(string)))
totalStr = ABCStr + abcStr
abcList = [ABCStr.lower(), abcStr]
shortFlag = 0 if len(abcList[0]) < len(abcList[1]) else 1
if len(abcList[shortFlag]) == 0: return 0, ''
filterList = sorted(set([x for x in abcList[shortFlag] if x in abcList[1 - shortFlag]]))
if len(filterList) == 0 : return 0, ''
cursor = 0
basket = [set()]
if len(filterList) == 1:
tempA = filterList[0]
return chr(ord(tempA) - 32) + tempA, ''
for a,b in zip(filterList[:-1],filterList[1:]):
if ord(b) - ord(a) == 1:
basket[cursor].add(a)
basket[cursor].add(b)
else:
cursor += 1
basket.append(set())
formBacket = [''.join(sorted(list(x))) for x in basket]
snakeStr = ''.join(list(map(lambda x: chr(ord(x) - 32) + x, sorted(formBacket, key=len, reverse=True)[0])))
existIndex = [totalStr.index(x) for x in snakeStr]
tranStr = ''.join([x for i,x in enumerate(totalStr) if i not in existIndex])
return snakeStr, tranStr
if __name__ == '__main__':
testString = r'SwSE$3454356DD$$#E#eswsxxsssAAWDxxSsdderfvcRFER65645hbg^^%%^UnbnvccTRChnyvcxcvVCFR'
# testString = sys.stdin.readline().strip()
snakeString, testString = findChar(testString)
if snakeString == 0:
print('Not Found')
else:
while snakeString != 0:
print(snakeString)
snakeString, testString = findChar(testString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment