第一行:模式串 `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
# 将一楼的大佬的代码改写成了Python, all pass, 感谢
import sys
def DFS(i,j):
if j==len(t):
S.add(i)
return
if i==len(s):
return
if s[i]==t[j]:
DFS(i+1, j+1)
elif t[j]=='*':
DFS(i, j+1)
DFS(i+1, j)
DFS(i+1, j+1)
return
while True:
line = sys.stdin.readline().strip()
if line == '':
break
t = line
s = input()
S = set()
flag = False
for i in range(len(s)):
# i is the start idx of s
# S includes all the end index it that s[i:it] matches t
if s[i]==t[0] or t[0]=='*':
DFS(i, 0)
if len(S) != 0:
flag = True
for it in sorted(S):
if it>i:
print(i,it-i)
S.clear()
if not flag:
print(-1, 0)