-6

这是我的代码:

keyinput = input() # I type 'appleandgold'

B = 'appleblue'

if keyinput in B: # Should actually be keyinput "intersects" B   
     print(keyinput) # Should print intersection

的结果(打印值)应该是keyinput = 'appleandgold',但我只能让它为.B = 'appleblue''apple'keyinput = 'apple'

4

2 回答 2

1

您可以在不导入任何模块的情况下尝试这样的事情:

s1='appleandgold'
s2='appleblue'

track=[]
for k in range(len(s1)):
    if k!=0:
        for ka in range(0,len(s1),k):
            if s1[ka:ka+k] in s2:
                track.append((len(s1[ka:ka+k]),s1[ka:ka+k]))
print(max(track)[1])

输出:

apple
于 2018-02-01T05:27:45.277 回答
0

您似乎在寻找最长的公共子字符串。标准库包含difflib用于此的模块:

from difflib import SequenceMatcher

keyinput = input()  # I type "appleandgold"

B = "appleblue"

match = SequenceMatcher(a=keyinput, b=B).find_longest_match(
    0,
    len(keyinput),
    0,
    len(B),
)
print(keyinput[match.a:match.a+match.size])  # prints "apple"
于 2018-01-31T08:43:04.233 回答