06250: 字符串最大跨距
http://cs101.openjudge.cn/dsapre/06250/
有三个字符串S,S1,S2,其中,S长度不超过300,S1和S2的长度不超过10。想检测S1和S2是否同时在S中出现,且S1位于S2的左边,并在S中互不交叉(即,S1的右边界点在S2的左边界点的左侧)。计算满足上述条件的最大跨距(即,最大间隔距离:最右边的S2的起始点与最左边的S1的终止点之间的字符数目)。如果没有满足条件的S1,S2存在,则输出-1。
例如,S = "abcd123ab888efghij45ef67kl", S1="ab", S2="ef",其中,S1在S中出现了2次,S2也在S中出现了2次,最大跨距为:18。
输入
三个串:S, S1, S2,其间以逗号间隔(注意,S, S1, S2中均不含逗号和空格);
输出
S1和S2在S最大跨距;若在S中没有满足条件的S1和S2,则输出-1。
样例输入
abcd123ab888efghij45ef67kl,ab,ef样例输出
18python
# 23n2300017735(夏天明BrightSummer)
def find(s, pat):
nex = [0]
for i, p in enumerate(pat[1:], 1):
tmp = nex[i-1]
while True:
if p == pat[tmp]:
nex.append(tmp+1)
break
elif tmp:
tmp = nex[tmp-1]
else:
nex.append(0)
break
j = 0
for i, char in enumerate(s):
while True:
if char == pat[j]:
j += 1
if j == len(pat):
return i
break
elif j:
j -= nex[j]
else:
break
s, p1, p2 = input().split(',')
try:
assert((ans := len(s)-find(s, p1)-find(s[::-1], p2[::-1])-2) >= 0)
print(ans)
except (TypeError, AssertionError):
print(-1)