- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 74 lines of Python from the credited upstream file abc307_c.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4def find_black_pos(h):5 pos = []6 7 for i in range(h):8 si = input().rstrip()9 10 for j, sij in enumerate(si):11 if sij == "#":12 pos.append((i, j))13 14 return pos15 16 17def main():18 import sys19 20 input = sys.stdin.readline21 22 ha, wa = map(int, input().split())23 black_pos_a = find_black_pos(ha)24 25 hb, wb = map(int, input().split())26 black_pos_b = find_black_pos(hb)27 28 hx, wx = map(int, input().split())29 black_pos_x = set(find_black_pos(hx))30 31 def f(i, j, sheet):32 ok = True33 candidates = []34 35 for si, sj in sheet:36 ni, nj = i + si, j + sj37 38 if not (0 <= ni < hx and 0 <= nj < wx):39 ok = False40 break41 if not (ni, nj) in black_pos_x:42 ok = False43 break44 45 candidates.append((ni, nj))46 47 return ok, candidates48 49 a_count, b_count = 0, 050 remains = set(black_pos_x)51 52 for i in range(-hx, hx):53 for j in range(-wx, wx):54 ok, candidates_a = f(i, j, black_pos_a)55 56 if ok:57 a_count += 158 remains -= set(candidates_a)59 60 ok, candidates_b = f(i, j, black_pos_b)61 62 if ok:63 b_count += 164 remains -= set(candidates_b)65 66 if len(remains) == 0 and a_count >= 1 and b_count >= 1:67 print("Yes")68 else:69 print("No")70 71 72if __name__ == "__main__":73 main()74