擅长:python、mysql、java
<p>以下是另一种基于@pm2ring答案的方法:</p>
<pre><code>S ='what about the lazy brown fox that jumped over the other brown one which lazy dog ate the food of the fox'
Q ='lazy brown dog'
import itertools
track={}
for index,value in enumerate(S.split()):
if value in Q:
if value not in track:
track[value]=[index]
else:
track[value].append(index)
combination = [(min(item),max(item)) for item in itertools.product(*track.values())]
result=min([(i[1]-i[0],(i[0],i[1])) for i in combination if set(Q.split()).issubset(S.split()[i[0]:i[1]+1])])
print(result[1])
</code></pre>
<p>输出:</p>
<pre><code>(11, 15)
</code></pre>