题解 | 特殊城市
特殊城市
https://www.nowcoder.com/practice/46f68af83db74b709a788dedb656c5f9
import sys
from collections import defaultdict
N = int(input())
map_sc = defaultdict(int)
res = 0
for _ in range(N):
s,c = input().split()
map_sc[(s[0:2],c)] += 1 #在字典中记录输出城市+州元组对出现的次数
#
for (s,c) in map_sc.keys():
#通过> 遍历一半儿元素,防止遍历(s,c)和(c,s)计算重复
if s > c and (c,s) in map_sc.keys() :
res += map_sc[(s,c)] * map_sc[(c,s)]
print(res)
查看27道真题和解析