import sys def zhuan(s): if s == None or len(s) == 0: return 0 n=len(s) a=[] for i in range(n,0,-1): a.append(s[i-1]) optp = [[0 for col in range(n+1)] for raw in range(n+1)] for i in range(1, n + 1): for j in range(1, n + 1): if (s[i-1]==a[j-1]): optp[i][j]=optp[i-1][j-1]+1 else: optp[i][j]=max(optp[i-1][j],optp[i][j-1]) return n-optp[-1][-1] if __name__ == '__main__': while True: line = sys.stdin.readline().strip() if not line: break print (zhuan(line))
python 动态规划之最大字串
最新推荐文章于 2023-08-29 22:28:21 发布