Skip to content

Instantly share code, notes, and snippets.

@andigena
Created July 4, 2016 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andigena/049b2bdb299dba7895a4400311b76f76 to your computer and use it in GitHub Desktop.
Save andigena/049b2bdb299dba7895a4400311b76f76 to your computer and use it in GitHub Desktop.
import sys
sys.setrecursionlimit(10000)
files = ['A-small-practice.in', 'A-large-practice.in']
def last_word(S, lastw):
if S == '':
return lastw
else:
return last_word(S[1:], S[0] + lastw if S[0] >= lastw[0] else lastw + S[0])
def solve(fn):
sol = ''
with open(fn) as f:
next(f)
for case, line in enumerate(f, 1):
S = line.strip()
curr = last_word(S[1:], S[0])
sol += 'Case #{}: {}\n'.format(case, curr)
with open(fn + '.sol', 'wt') as f:
f.write(sol)
print(sol)
for fn in files:
solve(fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment