python求最长子串
来源:互联网 发布:数据分析与处理 编辑:程序博客网 时间:2024/06/18 15:24
一、 假定s是小写字母的字符串。编写程序,输出s的最长子串(子串必须是字母顺序)。
如:s = 'azcbobobegghakl',则应输出:
Longest substring in alphabetical order is: beggh
又如:s = 'abcbcd',则应输出第一个子串:
Longest substring in alphabetical order is: abc
#-*- coding:UTF-8 -*-__author__ = "zhangguodong"__time__ ="2017.10.11"from string import lowercasedef max_sub(s): cont = [] sub = [] for i in s: if len(sub) >= 1 and lowercase.index(sub[-1]) <= lowercase.index(i): sub.append(i) cont.append(''.join(sub)) else: sub = [] sub.append(i) # print cont cont = sorted(cont, key=len, reverse=True) return cont[0]s1 = 'azcbobobegghakl's2 = 'abcbcd'result1 = max_sub(s1)print "The longest substring of \"%s\" in alphabetical order is:%s" % (s1,result1)result2 = max_sub(s2)print "The longest substring of \"%s\" in alphabetical order is:%s" % (s1,result2)
阅读全文
0 0
- python求最长子串
- python版本--求最长子串
- python实现求两个字符串的最长公共子串
- python实现求字符串最长公共子串
- 求两个字符串的最长公共子串 python实现
- 求最长回文子串
- 求最长子串[C++]
- 求最长公共子串
- 求最长重复子串
- 求最长回文子串
- 求最长公共子串
- 求最长公共子串
- 求最长公共子串
- 求最长回文子串
- 求最长回文子串
- 求最长公共子串
- 求最长回文子串
- 求最长公共子串
- MetaProgramming Chapter5
- 梯度下降法
- 达内课程-PrintStream
- 微信公众号机器人开发
- eclipse下SVN同步时忽略target文件夹
- python求最长子串
- Echarts实现地图点击与折线图联动
- 修改Tomcat下项目配置文件
- postman的安装与使用(模拟请求)
- MetaProgramming Chapter6
- switch case
- LINUX下EXT3文件系统大小有2T限制
- java中方法不要写太长的真正原因-DontCompileHugeMethods
- volley学习那点事