一开始stu用列表测试点5会超时,那换字典就ok。
测试点1输入如:
1 1
1 0
wow1
peo, cou = map(int, input().split(' '))
stu = {}
for i in range(cou):
x, n = map(int, input().split(' '))
if n != 0:#防止测试点1非零返回
name = input().split(' ')
for j in name:
if j not in stu:
stu[j] = [x]
else:
stu[j].append(x)
allnames = input().split(' ')
for i in range(peo):
if allnames[i] not in stu:
print(allnames[i], 0)
else:
temp = stu[allnames[i]]
temp.sort()
print(allnames[i], len(temp), end=' ')
for j in range(len(temp)):
print(temp[j], end='\n' if j == len(temp)-1 else ' ')
赋一下stu用列表测试点5超时版本
peo, cou = map(int, input().split(' '))
stu = []
for i in range(cou):
x, n = map(int, input().split(' '))
if n != 0:
name = input().split(' ')
for j in name:
if j not in stu:
stu.extend([j, [x]])
else:
stu[stu.index(j)+1].append(x)
allnames = input().split(' ')
for i in range(peo):
if allnames[i] not in stu:
print(allnames[i], 0)
else:
temp = stu[stu.index(allnames[i])+1]
temp.sort()
print(allnames[i], len(temp), end=' ')
for j in range(len(temp)):
print(temp[j], end='\n' if j == len(temp)-1 else ' ')