MITx: 6.00.1x Alphabetical Substrings (python)

本文介绍了一种算法,用于找出给定字符串中最长的按字母顺序排列的子串。通过迭代和比较,该程序能有效找到符合条件的最长子串,并在出现长度相等的情况下优先输出首次出现的子串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Assume s is a string of lower case characters.

Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print

Longest substring in alphabetical order is: beggh

 

In the case of ties, print the first substring.  For example, if s = 'abcbcd', then your program should print

Longest substring in alphabetical order is: abc

For problems such as these, do not include raw_input statements or define the variable s in any way. Our automated testing will provide a value of s for you - so the code you submit in the following box should assume s is already defined. If you are confused by this instruction, please review L4 Problems 10 and 11 before you begin this problem set.

Note: This problem is fairly challenging. We encourage you to work smart. If you've spent more than a few hours on this problem, we suggest that you move on to a different part of the course. If you have time, come back to this problem after you've had a break and cleared your head.


# from string import lowercase
s = raw_input("Please input a String:")

from itertools import count
def find_substring(input_string):
	maxsubstr = input_string[0:0]
	for start in range(len(input_string)):
		for end in count(start+len(maxsubstr)+1):
			substr = input_string[start:end]
			if len(substr)!=(end - start):
				break
			if sorted(substr) == list(substr):
				maxsubstr = substr
	return maxsubstr

log_substr = find_substring(s)
print ("Longest substring in alphabetical order is:"+log_substr)
#lowe = 'abcdefghijklmnopqrstuvwxyz'  
# cont = []
# sub = []
# for i in s:
#     if len(sub) >= 1 and lowe.index(sub[-1]) + 1 != lowe.index(i):
#             cont.append(''.join(sub))
#             sub = []
#     sub.append(i)
 
# cont = sorted(cont, key = len, reverse=True)
# print cont[0]


# cont = []
# sub = []
# for i in s:
#     if len(sub) >= 1 and lowercase.index(sub[-1]) + 1 != lowercase.index(i):
#             cont.append(''.join(sub))
#             sub = []
#     sub.append(i)
 
# cont = sorted(cont, key = len, reverse=True)
# print cont[0]
# counter = 0
# i = 0
# length = len(s)
# temp = s[0]
# substr = s[0]

# while (i<=length-1):
#     if s[i]<s[i+1]:
#         temp += s[i+1]
#         counter += 1
#     elif s[i]>s[i+1]:
#         if len(substr)<len(temp):
#             substr = temp
#         temp = s[i+1]
#     i = i + 1
# if len(temp)>len(substr):
#     substr = temp
# print 'Longest substring in alphabetical order is:',substr


#s = 'cyqfjhcclkbxpbojgkar'




# def long_alphabet(input_string):
#     maxsubstr = input_string[0:0] # empty slice (to accept subclasses of str)
#     for start in range(len(input_string)): # O(n)
#         for end in count(start + len(maxsubstr) + 1): # O(m)
#             substr = input_string[start:end] # O(m)

#             if len(substr) != (end - start): # found duplicates or EOS
#                 break
#             if sorted(substr) == list(substr):
#                 maxsubstr = substr
#     return maxsubstr

# bla = (long_alphabet(s))
# print "Longest substring in alphabetical order is: %s" %bla


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值