I’m working on the 2016 December Silver Problem Cities and States. I’ve read and understood the solution, but it feels unnecessarily complicated. I think my solution should work as well, but it only passes 2 testcases, although it passed every case I came up with. My code is below.
import sys
sys.stdin = open("citystate.in", "r")
sys.stdout = open("citystate.out", "w")
n = int(input())
pair1 = set()
pair2 = set()
for _ in range(n):
c,s = input().split()
c = c[:2]
pair1.add(c+s)
pair2.add(s+c)
count = 0
for pair in pair1:
if pair[:2] != pair[-2:] and pair in pair2:
count += 1
print(count//2)