1. 程式人生 > 其它 >牛客華為機試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