其他分享
首页 > 其他分享> > 牛客华为机试HJ25

牛客华为机试HJ25

作者:互联网

原题传送门

1. 题目描述

2. Solution 1

import sys

if sys.platform != "linux":
    file_in = open("input/HJ25.txt")
    sys.stdin = file_in


def solve(I, R):
    uniq_r = list(set(R))
    uniq_r.sort(key=lambda a: int(a))
    res = dict()
    for x in uniq_r:
        res[x] = dict()
    # uniq_r = [0, 3, 6] -> res = dict(0=dict(), 3=dict(), 6=dict())
    for i in range(len(I)):
        for k in res:
            if k in I[i]:
                res[k][i] = I[i]
    cnt = 0
    content = []
    for r, map_r in res.items():
        if not map_r:
            continue
        cnt += 2 * (len(map_r) + 1)
        content.append(r)
        content.append(str(len(map_r)))
        for idx, num in map_r.items():
            content.append(str(idx))
            content.append(num)
    content.insert(0, str(cnt))
    print(" ".join(content))


while True:
    try:
        I = input().strip().split()
        R = input().strip().split()
        I.pop(0)
        R.pop(0)
        solve(I, R)
    except:
        break

标签:map,res,content,牛客,dict,机试,uniq,HJ25,append
来源: https://www.cnblogs.com/junstat/p/16163641.html