题解 | 特殊城市

特殊城市

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)

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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