Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- from sys import argv
- T = int(input())
- DEBUG=False
- if len(argv) > 1:
- DEBUG=True
- def debug(*args):
- if DEBUG:
- print(*args)
- def get_secret(line):
- a = line.index('>')
- b = line.index('<')
- if b > a:
- return line[a+1:b]
- def sort_string(s):
- return ''.join(sorted(s))
- def get_unscrambled_line(line, key):
- return ''.join([line[key[i]] for i in range(len(line))])
- def solve(H, W, col_shuffle, row_shuffle):
- possibles = [set(range(W)) for _ in range(W)]
- distinct_rows = set(row_shuffle)
- # anagram2rows = defaultdict(list)
- # for row in distinct_rows:
- # anagram2rows[sort_string(row)].append(row)
- # assert all(len(v) == 1 for v in anagram2rows.values())
- anagram2rows = {sort_string(row): row for row in distinct_rows}
- for row in col_shuffle[1:]:
- actual_row = anagram2rows[sort_string(row)]
- for col in range(W):
- if len(possibles[col]) == 1:
- continue
- c = actual_row[col]
- next_possible = set()
- for gc in possibles[col]:
- if row[gc] == c:
- next_possible.add(gc)
- possibles[col] = next_possible
- assert all(len(p) == 1 for p in possibles)
- key = [list(p)[0] for p in possibles]
- for line in col_shuffle:
- debug(get_unscrambled_line(line, key))
- top_line = get_unscrambled_line(col_shuffle[0], key)
- return get_secret(top_line)
- for case in range(1, T+1):
- col_shuffle = []
- while True:
- s = input()
- #print(s)
- if '=====' in s:
- break
- col_shuffle.append(s)
- H = len(col_shuffle)
- W = len(col_shuffle[0])
- row_shuffle = []
- for _ in range(H-1):
- row_shuffle.append(input())
- input()
- s = solve(H, W, col_shuffle, row_shuffle)
- print(f"Case #{case}:", s)
Add Comment
Please, Sign In to add comment