这道题翻看了网上很多版本,貌似都没看到过怎么用正则表达式的写法,不知道是不是我的搜索姿势不太对。
()
里的字母提取出来。我写的正则表达筛选有点丑,如果有更简洁的欢迎指出。flag = re.compile(r'[(][0-9](.*?)[)]')
[' a c', ' b d', ' a c', ' a b e']
replace()
函数用空代替空格输入:
3 4
3 4 2 a c
2 5 1 b
5 3 2 b c
1 5 4 a b d e
(2 a c) (2 b d) (2 a c) (3 a b e)
(2 a c) (1 b) (2 a b) (4 a b d e)
(2 b d) (1 e) (2 b c) (4 a b c d)
输出:
3
6
5
2 2 3 4
import re
n, m = map(int, input().split())
score = [] # 每题的分数
true_ans = [] # 每题的正确答案
fault = [0 for i in range(m)] # 统计错误信息
for i in range(m):
info = input().split()
score.append(int(info[0]))
true_ans.append("".join(info[3:]))
# true_ans 长这样['ac','b','bc','abde']
for i in range(n):
stu_score = 0
answer = input()
flag = re.compile(r'[(][0-9](.*?)[)]')
answer = flag.findall(answer) # answer是个列表,可以输出看看内容
for j in range(m):
answer[j] = answer[j].replace(" ", "") # 空替换空格
if answer[j] != true_ans[j]:
fault[j] += 1
else:
stu_score += score[j]
print(stu_score)
mx = max(fault)
if mx == 0:
print("Too simple")
else:
ans = [str(mx)]
for index, num in enumerate(fault, 1):
if num == mx:
ans.append(str(index))
print(" ".join(ans))