Python 字符串中子字符串的最小范围
我有一个字符串S(单词从0开始索引)和一个子字符串Q。我希望在S中找到包含Q中所有单词的最小范围[L,R]。Q中没有重复的单词。我如何处理这个问题 比如说, 输入: S:跳到另一只棕色狐狸身上的那只懒棕色狐狸呢?那只懒狗吃了狐狸的食物 Q:懒惰的棕色狗 输出:[11,15] 我的代码:Python 字符串中子字符串的最小范围,python,string,range,substring,Python,String,Range,Substring,我有一个字符串S(单词从0开始索引)和一个子字符串Q。我希望在S中找到包含Q中所有单词的最小范围[L,R]。Q中没有重复的单词。我如何处理这个问题 比如说, 输入: S:跳到另一只棕色狐狸身上的那只懒棕色狐狸呢?那只懒狗吃了狐狸的食物 Q:懒惰的棕色狗 输出:[11,15] 我的代码: S = raw_input().strip().split(' ') Q = raw_input().strip().split(' ') count = [0 for x in range(len(Q))]
S = raw_input().strip().split(' ')
Q = raw_input().strip().split(' ')
count = [0 for x in range(len(Q))]
smallest_index = [0 for x in range(len(Q))]
largest_index = [0 for x in range(len(Q))]
for i in range(len(S)):
for j in range(len(Q)):
if S[i] == Q[j]:
count[j] += 1
if count[j] <= 1:
smallest_index[j] = i
largest_index[j] = i
if count[j] > 1:
largest_index[j] = i
largest_index.sort()
print "[%d," % largest_index[0],
print "%d]" % largest_index[len(Q)-1]
S=原始输入().strip().split(“”)
Q=原始输入().strip().split(“”)
计数=[0表示范围内的x(len(Q))]
最小_指数=[0表示范围内的x(len(Q))]
最大_指数=[0表示范围内的x(len(Q))]
对于范围内的i(len(S)):
对于范围内的j(len(Q)):
如果S[i]==Q[j]:
计数[j]+=1
如果计数[j]1:
最大_指数[j]=i
最大索引。排序()
打印“[%d,”%u索引[0],
打印“%d]”最大索引[len(Q)-1]
以下是另一种基于@PM 2Ring答案的方法:
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])
输出:
(11, 15)
以下是另一种基于@PM 2Ring答案的方法:
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])
输出:
(11, 15)
这段代码不是特别有效,但它确实工作正常。也许有人会想出一种比使用
产品
更好的方法来处理职位信息。同时,您可以使用此代码测试其他算法
from itertools import product
def words_range(src, query):
# Create a dict to store the word positions in src of each query word
pos = {s: [] for s in query}
for i, s in enumerate(src):
if s in pos:
pos[s].append(i)
print(pos)
# Find all the ranges that hold all the query word
ranges = ((min(t), max(t)) for t in product(*pos.values()))
# Find the smallest range
return min(ranges, key=lambda t:t[1] - t[0])
# Test
src = '''what about the lazy brown fox that jumped over the other
brown one which lazy dog ate the food of the fox'''.split()
for i, s in enumerate(src):
print(i, s)
query = 'lazy brown dog'.split()
print(words_range(src, query))
query = 'the lazy brown fox'.split()
print(words_range(src, query))
输出
0 what
1 about
2 the
3 lazy
4 brown
5 fox
6 that
7 jumped
8 over
9 the
10 other
11 brown
12 one
13 which
14 lazy
15 dog
16 ate
17 the
18 food
19 of
20 the
21 fox
{'lazy': [3, 14], 'brown': [4, 11], 'dog': [15]}
(11, 15)
{'the': [2, 9, 17, 20], 'lazy': [3, 14], 'brown': [4, 11], 'fox': [5, 21]}
(2, 5)
这段代码不是特别有效,但它确实工作正常。也许有人会想出一种比使用
产品
更好的方法来处理职位信息。同时,您可以使用此代码测试其他算法
from itertools import product
def words_range(src, query):
# Create a dict to store the word positions in src of each query word
pos = {s: [] for s in query}
for i, s in enumerate(src):
if s in pos:
pos[s].append(i)
print(pos)
# Find all the ranges that hold all the query word
ranges = ((min(t), max(t)) for t in product(*pos.values()))
# Find the smallest range
return min(ranges, key=lambda t:t[1] - t[0])
# Test
src = '''what about the lazy brown fox that jumped over the other
brown one which lazy dog ate the food of the fox'''.split()
for i, s in enumerate(src):
print(i, s)
query = 'lazy brown dog'.split()
print(words_range(src, query))
query = 'the lazy brown fox'.split()
print(words_range(src, query))
输出
0 what
1 about
2 the
3 lazy
4 brown
5 fox
6 that
7 jumped
8 over
9 the
10 other
11 brown
12 one
13 which
14 lazy
15 dog
16 ate
17 the
18 food
19 of
20 the
21 fox
{'lazy': [3, 14], 'brown': [4, 11], 'dog': [15]}
(11, 15)
{'the': [2, 9, 17, 20], 'lazy': [3, 14], 'brown': [4, 11], 'fox': [5, 21]}
(2, 5)
这是的一个稍微高效的版本,将对
product
的调用替换为循环:
from itertools import product
def words_range(src, query):
query = set(query)
# Create a dict to store the word positions in src of each query word
pos = {s: [] for s in query}
for i, s in enumerate(src):
if s in pos:
pos[s].append(i)
# Find all the ranges that hold all the query word
# We'll iterate over the input string and keep track of
# where each word appeared last
last_pos = {}
ranges = []
for i, word in enumerate(src):
if word in query:
last_pos[word] = i
if len(last_pos) == len(query):
ranges.append( (min(last_pos.values()), i) )
# Find the smallest range
return min(ranges, key=lambda t:t[1] - t[0])
它不是完全线性的时间(因为循环中的
min(last_pos.values())
),但它是朝着正确方向迈出的一步。可能有一种方法可以消除min
调用(我现在想不起来),这会使它线性化。这是的一个稍微高效的版本,用一个循环代替对产品的调用:
from itertools import product
def words_range(src, query):
query = set(query)
# Create a dict to store the word positions in src of each query word
pos = {s: [] for s in query}
for i, s in enumerate(src):
if s in pos:
pos[s].append(i)
# Find all the ranges that hold all the query word
# We'll iterate over the input string and keep track of
# where each word appeared last
last_pos = {}
ranges = []
for i, word in enumerate(src):
if word in query:
last_pos[word] = i
if len(last_pos) == len(query):
ranges.append( (min(last_pos.values()), i) )
# Find the smallest range
return min(ranges, key=lambda t:t[1] - t[0])
它不是完全线性的时间(因为循环中的min(last_pos.values())
),但它是朝着正确方向迈出的一步。可能有一种方法可以消除min
调用(我现在想不起来),这会使此线性化。显示您尝试过的代码?为什么是否决票?有可能是因为您发布了一个没有代码的广泛问题而导致否决票。您的问题现在好多了,但您需要解释您发布的代码有什么问题。我的问题是时间复杂性。我正在运行两个循环。如果S和Q的大小很大呢?我怎样才能使它更有效率,这很公平。为什么收集最小的索引值,但从不使用它们?但除此之外,我不认为你的算法总能找到正确的最小解。最小的范围可以是使用最大的索引,最小的索引,或者中间的某个地方。显示你尝试的代码。为什么要投票?因为你发布了一个没有代码的广泛问题,你可能得到了一个否决票。您的问题现在好多了,但您需要解释您发布的代码有什么问题。我的问题是时间复杂性。我正在运行两个循环。如果S和Q的大小很大呢?我怎样才能使它更有效率,这很公平。为什么收集最小的索引值,但从不使用它们?但除此之外,我不认为你的算法总能找到正确的最小解。最小范围可以是使用最大指数、最小指数或中间的某个区间。