【字符串序列判定】Python 实现

B站影视 欧美电影 2025-05-25 14:43 2

摘要:def find_last_subsequence(target, source): m = len(target) n = len(source) if m == 0: return 0 if n == 0: return -1 # 逆向匹配,找到最后一个子

def find_last_subsequence(target, source): m = len(target) n = len(source) if m == 0: return 0 if n == 0: return -1 # 逆向匹配,找到最后一个子序列的起始位置 i = m - 1 j = n - 1 while i >= 0 and j >= 0: if target[i] == source[j]: i -= 1 j -= 1 if i == -1: return j + 1 else: return -1# 读取输入target = input.stripsource = input.strip# 调用函数并输出结果result = find_last_subsequence(target, source)print(result)

来源:搞笑与科技

相关推荐