Created
December 9, 2013 07:15
-
-
Save cocodrips/7868493 to your computer and use it in GitHub Desktop.
#rprocon I問題を解いてみた ※まちがいだらけ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solve(before, after): | |
pairs_dict = {} | |
for i in xrange(len(before)): | |
if not pairs_dict.has_key(before[i]): | |
pairs_dict[before[i]] = after[i] | |
else: | |
if pairs_dict[before[i]] != after[i]: | |
return "impossible" | |
if len(pairs_dict) >= 26: | |
return "impossible" | |
swap_count = 0 | |
for k, v in pairs_dict.items(): | |
if pairs_dict[k] == v: | |
if pairs_dict.has_key(v) and pairs_dict[v] == k: | |
swap_count += 1 | |
swap_count /= 2 | |
return str(len(pairs_dict) + swap_count) | |
if __name__ == "__main__": | |
with open('inputI.txt', 'r') as infile, open('outputI.txt', 'w') as outfile: | |
data = [] | |
for line in infile: | |
data.append(line.split()) | |
num = data.pop(0) | |
num = int(num[0]) | |
for _ in xrange(num): | |
[before_word] = data.pop(0) | |
[after_word] = data.pop(0) | |
outfile.write(solve(before_word, after_word) + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment