https://www.nowcoder.com/practice/6463cf20047a4feab35a1379c12fc811?tpId=390&tqId=11522617&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D390

需要注意对于完美匹配情况,位置权重不是按照匹配的位置计算,而是按照第一个位置计算。

import sys
import re
from collections import defaultdict
import bisect
import math
data = sys.stdin.read().split('|')

def sanitize(s):
    return re.sub(r'^[^\w\d]+|[^\w\d]+$', '', s)
scroll_text = defaultdict(lambda : [])
for i, c in enumerate(data[0].split()):
    scroll_text[sanitize(c).lower()].append(i)
res = []
L = len(data[0].split())
for incant in data[1:]:
    incants = [sanitize(c).lower() for c in incant.split()]
    k = len(incants)
    i, W = 0, 0
    vis = set()
    for c in incants:
        if c in scroll_text:
            i += 1
            if c not in vis:
                W += 1 - scroll_text[c][0] / (L-1) if L > 1 else 1
                vis.add(c)
    if i ==0 :
        res.append(0)
    elif i < k:
        res.append(0.6 * i / k * W)
    else:
        pos, perfect = -1, True
        for c in incants:
            if scroll_text[c][-1] > pos:
                ind = bisect.bisect_left(scroll_text[c], pos)
                pos = scroll_text[c][ind]
            else:
                perfect = False
                break
        if perfect:
            res.append(W)
        else:
            res.append(0.8 * W)
print('|'.join(map(lambda x: '{:.4f}'.format(math.floor(x*10000)/10000), res)))