T1
:
找到包含最多k个不同字符的最长子字符串T的长度。
T2
:
找出最短的子串T的长度,其中至少包含k个不同的字符。
T1
是T2
的强对偶还是弱对偶?本质上,T1
和T2
在问同一件事吗?
答案 0 :(得分:1)
我将用否回答两个问题。考虑示例字符串:
"This is an example. Don't ask me about the example string."
对于k = 15,T1为"'t ask me about the example st"
,T2为"xample. Don't ask"
。
这些是使用以下Python程序计算的:
1 'T' 'T'
2 't t' 'Th'
3 'is is ' 'Thi'
4 'his is ' 'This'
5 'is is an ' 'This '
6 'his is an ' 'n exam'
7 'is is an exa' 'n examp'
8 'his is an exa' 'n exampl'
9 ' me about the e' 'xample. D'
10 ' me about the exam' 'xample. Do'
11 't ask me about the e' 'xample. Don'
12 't ask me about the exam' "xample. Don'"
13 "'t ask me about the exam" "xample. Don't"
14 't ask me about the example st' 'xample string.'
15 "'t ask me about the example st" "xample. Don't ask"
16 "is is an example. Don't ask me a" "ple. Don't ask me abou"
17 "is is an example. Don't ask me abo" 'bout the example string'
18 "s an example. Don't ask me about the example st" 'bout the example string.'
19 "his is an example. Don't ask me about the example st" 'k me about the example string.'
x = "This is an example. Don't ask me about the example string."
def factors(s):
return [s[i:j] for i in range(len(s)+1) for j in range(i, len(s)+1)]
for k in range(20):
t1 = max([f for f in factors(x) if len(set(f)) <= k], key=len)
t2 = min([f for f in factors(x) if len(set(f)) >= k], key=len)
print k, repr(t1), repr(t2)
哪些印刷品:
0 '' ''
1 'T' 'T'
2 't t' 'Th'
3 'is is ' 'Thi'
4 'his is ' 'This'
5 'is is an ' 'This '
6 'his is an ' 'n exam'
7 'is is an exa' 'n examp'
8 'his is an exa' 'n exampl'
9 ' me about the e' 'xample. D'
10 ' me about the exam' 'xample. Do'
11 't ask me about the e' 'xample. Don'
12 't ask me about the exam' "xample. Don'"
13 "'t ask me about the exam" "xample. Don't"
14 't ask me about the example st' 'xample string.'
15 "'t ask me about the example st" "xample. Don't ask"
16 "is is an example. Don't ask me a" "ple. Don't ask me abou"
17 "is is an example. Don't ask me abo" 'bout the example string'
18 "s an example. Don't ask me about the example st" 'bout the example string.'
19 "his is an example. Don't ask me about the example st" 'k me about the example string.'