题解 | #查找两个字符串a,b中的最长公共子串#
查找两个字符串a,b中的最长公共子串
https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506
tmp1=input() tmp2=input() if len(tmp1)>len(tmp2): tmp1,tmp2=tmp2,tmp1 a=list(tmp1) b=list(tmp2) dp=[[0 for _ in range(len(b)+1)]for _ in range(len(a)+1)] ss=[["" for _ in range(len(b)+1)]for _ in range(len(a)+1)] max_s,max_len="",0 for i in range(1,len(a)+1): for j in range(1,len(b)+1): if a[i-1]==b[j-1]: dp[i][j]=1+dp[i-1][j-1] ss[i][j]=ss[i-1][j-1]+a[i-1] if dp[i][j]>max_len: max_len=dp[i][j] max_s=ss[i][j] print(max_s)