参考回答:
#!/usr/bin/python#encoding:utf-8def lengthCa(arr):start = arr[0]end= arr[0]for i in arr:if i >start:start=iif i < end :end = ireturn start-end
#输入的第一个文本
text = 'ILM runs a batch processing environment capable of modeling, rendering and compositing tens of thousands of motion picture frames per day. Thousands of machines running Linux, IRIX, Compaq Tru64, OS X, Solaris, and Windows join together to provide a production pipeline used by ~800 users daily. Speed of development is key, and Python was a faster way to pre (and re-pre) the programs that control this production pipeline.'
#输入的第二个文本
keywords='a of'newtext=text.split(' ')newkeys = keywords.split(' ')textLen = len(newtext)array=[]#把index计算出来,用做最优路径规划使用
for i in newkeys:dan=[]for j in range(textLen):if i == newtext[j]:dan.append(j)array.append(dan)print(array)
#最优规划开始
caculateArray=[]for n in array[0]:temp=[]temp.append(n)caculateArray.append(n)flag = 0for n in array:if array.index(n) == 0:continuetemparr=[]for m in caculateArray:#遍历当前最短路径index = caculateArray.index(m)#计算当前路径的index值tempminlen=1000000tempminarr=[]for j in n:#计算当前最短路径,添加下一个节点if flag ==0: temparr.append(m) else: for x in m: temparr.append(x) temparr.append(j) if lengthCa(temparr)<tempminlen : tempminlen=lengthCa(temparr) tempminarr=temparr temparr=[] caculateArray[index]=tempminarr print(caculateArray) flag+=1 tempminlen=1000000 tempminarr=[] #找出最终所有解里的最优解,为tempminarr for n in caculateArray: if lengthCa(n)<tempminlen : tempminlen=lengthCa(n) tempminarr=n #计算tempminarr的起点和重点,现在发现用min()和max()函数就可以了 start = tempminarr[0] end = tempminarr[0] for i in tempminarr: if start<i: start=i if end>i: end=i#输出起始位置和终止位置 print(start,end) for m in range(end,start+1): print newtext[m]



