由于python中的for循环不像C++这么灵活,因此该用枚举法实现该算法:
C="abcdefhe"
D="cdefghe"
m=0
n=len(C)
E=[]
b=0
while(m<n):
i=n-m
while(i>=0):
E.append(C[m:m+i])
i-=1
m+=1
for x in E:
a=0
if x in D:
a=len(x)
c=E.index(x)
if a > b:#保存符合要求的最长字符串长度和地址
b=a
d=c
if b>0:
print E[d]
C=