使用python

时间:2015-09-10 13:10:15

标签: python

s='sqrileksarrawixamwabj'
pos = 0
maxLen = 0
startPos = 0
endPos = 0


def last_pos(pos):
    if pos < (len(s) - 1):
        if s[pos + 1] >= s[pos]:
            pos += 1
            if pos == len(s)-1:
                return len(s)
            else:
                return last_pos(pos)
        return pos


for i in range(len(s)):
    if last_pos(i) != None:
        diff = last_pos(i) - i + 1
    if diff > maxLen:
        maxLen = diff
        startPos = i
        endPos = startPos + diff - 1

print "Longest substring in alphabetical order is:"+ s[startPos:endPos+1]

现在输出变为&#39; abj&#39;但是还有其他子字符串,例如&#39; eks&#39;并且&#39; amw&#39;。我想要的是输出应该是&#39; eks&#39;它首先作为s中的子串而不是&#39; abj&#39;这是最后出现的子字符串。应该在上面的代码中进行哪些修改,以便获得所需的输出?

0 个答案:

没有答案