用Python 找最长子串
来源:互联网 发布:沙滩鞋淘宝 编辑:程序博客网 时间:2024/06/05 16:22
题目选自edX公开课 MITx: 6.00.1x Introduction to Computer Science and Programming 课程 Week2 的Problem Set 1的第三题。下面是原题内容。
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, ifs = '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.
代码如下:
#判断一个字符串内的字母是否是按字母表顺序# 如IsStrIncre('abbcdg') 返回 True# IsStrIncre('abbadg') 返回 False# 如果只有一个字符,也返回False def IsStrIncre(s): for cnt in range(len(s) - 1): if len(s) == 1: return False elif s[cnt] > s[cnt+1]: return False return True s = 'abajsiesnwdw'# example codesubstr = ''for length in range(1, len(s)+1): firstflag = True # a flag to remember the first string that satisfied the requirements # and ignore the strings satisfied the requirements but appeared after for cnt in range(len(s)-length+1): if IsStrIncre(s[cnt: cnt+length]): if firstflag: substr = s[cnt: cnt+length] firstflag = False print 'Longest substring in alphabetical order is: ' + substr
- 用Python 找最长子串
- 找最长数字子串
- 回文数 找最长回文子串
- 找最长公共子串的函数
- python -- 最长公共子串
- python求最长子串
- 找字符串中最长不重复子串
- 需找字符串中重复的最长子串
- python版本--求最长子串
- python解决最长回文子串问题
- Python最长公共子串和最长公共子序列的实现
- 最长公共子串和最长公共子序列之Python实现
- 最长公共子序列-python
- 最长公共子序列 python
- python -- 最长公共子序列
- Longest Palindromic Substring 在一个字符串里找最长回文子串@LeetCode
- 一道面试题 - 找最长固定unique个数的子串
- Manacher算法——找字符串最长的回文子串
- makefile 链接线程 pthread
- 连分号区间
- android如何让service不被杀死
- POJ 2396 有源有汇有上下界可行流问题
- 第一周--日期结构体
- 用Python 找最长子串
- 程序员该怎么面试
- Android开发者应该知道的Android体系架构和开发库
- Intent的几种意图
- 按后退键退出到主界面
- 动态规划-jobdu-1547:出入栈
- IE6,IE7下text-indent消失问题
- 打开Eclipse时出现 "Failed to create the Java Virtual Machine
- 生活随笔与读书笔记20140302