字符串中出现相同并且长度最长的子串
2016-05-13 11:31
239 查看
题目描述:在字符串中找到出现相同的并且长度是最长的子串,输出该子串以及其首字符的位置。
例如mainStr="yyabcdabjcabceg",输出为"abc"和3。
分析:每次都找到最大长度的子串,然后正序和逆序查找该子串出现的位置。如果两者位置不相等,则说明两子串是相同的并且长度最长的子串,满足条件。否则继续遍历执行。
例如mainStr="yyabcdabjcabceg",输出为"abc"和3。
分析:每次都找到最大长度的子串,然后正序和逆序查找该子串出现的位置。如果两者位置不相等,则说明两子串是相同的并且长度最长的子串,满足条件。否则继续遍历执行。
bool findSameMaxSubstr(const string& mainStr, string &subStr, int index) { bool isFind = false; int len = mainStr.length(); for (int i = len; i > 1; i--) { for (int j = 0; j < len; j++) { int firstIndex = 0; int secondIndex = 0; if (i + j < len) { subStr = mainStr.substr(i, j); firstIndex = mainStr.find(subStr); secondIndex = mainStr.rfind(subStr); if (firstIndex != secondIndex) { index = firstIndex + 1; return true; } } } } }
相关文章推荐
- Mantle使用(摘自GitHub)
- FLY-BY,你不可不知的两大布线细节
- hdu1242(BFS+优先队列)
- Android HttpURLConnection post请求
- HDU 2181 哈密顿绕行世界问题
- leetcode 21. Merge Two Sorted Lists
- Unity5.x版本AssetBundle加载研究
- Cannot call sendError() after the response has been committed
- A class file was not written. The project may be inconsistent, if so try refreshing this project and
- 搭建nexus私服
- ubuntu 键盘布局修改
- [HTML5] Canvas绘制简单图片
- Android studio 生成自己的jar包
- iOS Xcode 自带crash 崩溃问题的追踪方法
- “附加数据库失败,版本为661,无法打开,支持655版本...”和“无法执行脚本,没有足够的内存继续执...”
- 最全的常用正则表达式大全——包括校验数字、字符、一些特殊的需求等等
- LCD_Drivers
- moon 流和文件
- setValue和setObject的区别
- SpringMVC通过注解@Value获取properties配置