题解 | 特殊城市

特殊城市

https://www.nowcoder.com/practice/46f68af83db74b709a788dedb656c5f9

N = int(input())
counts = {}  # S:C
# 或使用 counts = defaultdict(int) 设定默认值为0

for _ in range(N):
    S, C = input().split()
    prefix = S[:2]
    if not counts.get((prefix, C)):
        counts[(prefix, C)] = 0
    counts[(prefix, C)] += 1

res = 0
for (s1, s2) in counts.keys():
    # s1!=s2即可保证A,B来自不同的州
    if s1 < s2 and (s2, s1) in counts:
        res += counts[(s1, s2)] * counts[(s2, s1)]
print(res)

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务