Guest User

eldarverse-halloween-6C

a guest
Nov 3rd, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. from collections import defaultdict
  2. from sys import argv
  3. T = int(input())
  4.  
  5. DEBUG=False
  6. if len(argv) > 1:
  7.     DEBUG=True
  8.  
  9. def debug(*args):
  10.     if DEBUG:
  11.         print(*args)
  12.  
  13. def get_secret(line):
  14.     a = line.index('>')
  15.     b = line.index('<')
  16.     if b > a:
  17.         return line[a+1:b]
  18.  
  19. def sort_string(s):
  20.     return ''.join(sorted(s))
  21.  
  22. def get_unscrambled_line(line, key):
  23.     return ''.join([line[key[i]] for i in range(len(line))])
  24.  
  25. def solve(H, W, col_shuffle, row_shuffle):
  26.     possibles = [set(range(W)) for _ in range(W)]
  27.     distinct_rows = set(row_shuffle)
  28.     # anagram2rows = defaultdict(list)
  29.     # for row in distinct_rows:
  30.     #     anagram2rows[sort_string(row)].append(row)
  31.     # assert all(len(v) == 1 for v in anagram2rows.values())
  32.     anagram2rows = {sort_string(row): row for row in distinct_rows}
  33.     for row in col_shuffle[1:]:
  34.         actual_row = anagram2rows[sort_string(row)]
  35.         for col in range(W):
  36.             if len(possibles[col]) == 1:
  37.                 continue
  38.             c = actual_row[col]
  39.             next_possible = set()
  40.             for gc in possibles[col]:
  41.                 if row[gc] == c:
  42.                     next_possible.add(gc)
  43.             possibles[col] = next_possible
  44.     assert all(len(p) == 1 for p in possibles)
  45.     key = [list(p)[0] for p in possibles]
  46.     for line in col_shuffle:
  47.         debug(get_unscrambled_line(line, key))
  48.     top_line = get_unscrambled_line(col_shuffle[0], key)
  49.     return get_secret(top_line)
  50.  
  51. for case in range(1, T+1):
  52.     col_shuffle = []
  53.     while True:
  54.         s = input()
  55.         #print(s)
  56.         if '=====' in s:
  57.             break
  58.         col_shuffle.append(s)
  59.     H = len(col_shuffle)
  60.     W = len(col_shuffle[0])
  61.     row_shuffle = []
  62.     for _ in range(H-1):
  63.         row_shuffle.append(input())
  64.     input()
  65.     s = solve(H, W, col_shuffle, row_shuffle)
  66.     print(f"Case #{case}:", s)
Add Comment
Please, Sign In to add comment