第一行:模式串 `p` (长度不超过20)
第二行:目标串 `s` (长度不超过
)
对每一个匹配子串输出 `匹配起始位置 匹配的长度`(空格分隔)一行;若无匹配输出 `-1 0`。
shopee*.com shopeemobile.com
0 16
0 起始位置,16长度
*.com shopeemobile.com
0 16 1 15 2 14 3 13 4 12 5 11 6 10 7 9 8 8 9 7 10 6 11 5 12 4
o*m shopeemobile.com
2 5 2 14 7 9 14 2
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-12-25 原数据范围有误,已修正为,同时重造了数据;时间限制拓展为 2s,空间限制拓展为 256MB。
from sre_constants import JUMP
p = input()
s = input()
star_index = 0
for i in range(0, len(p)):
if p[i] == "*":
star_index = i
start = p[:star_index]
stop = p[star_index + 1 :]
# print(start, stop)
A = []
for i in range(0, len(s)):
for j in range(i, len(s)):
temp = str()
if start and stop:
temp = start + s[i : j] + stop
if not start and stop:
temp = s[i : j] + stop
if start and not stop:
temp = start + s[i : j]
if not start and not stop:
temp = s[i : j]
if temp in s:
if temp not in A:
A.append(temp)
# print(A)
if not A:
print("-1 0")
R = []
if A:
for i in range(0, len(A)):
# print(A[i])
if len(A[i]) >= 2:
first = A[i][0]
second = A[i][1]
j = 0
while True:
if s[j] == first:
if s[j + 1]== second:
R.append([j, len(A[i])])
break
j += 1
if len(A[i]) == 1:
first = A[i][0]
j = 0
while True:
if s[j] == first:
R.append([j, 1])
break
j += 1
R.sort()
# print(R)
if R:
for i in R:
t = str(i[0]) + " " + str(i[1])
print(t)