找出一个字符串中的最长奇数回文。在控制台输入输出。
来源:互联网 发布:mysql导出数据库命令 编辑:程序博客网 时间:2024/06/10 16:00
找出一个字符串中的最长奇数回文。在控制台输入输出。例如,字符串mabcbatqabccba中,最长回文为abcba,类似abccba不算.
暴力法
//// main.cpp// str//// Created by Bryan on 14-6-28.// Copyright (c) 2014年 Bryan. All rights reserved.//#include <iostream>#include <string>#include <vector>#include <algorithm>#include <fstream>#include <list>using namespace std;bool IsPalindromicStr(string s){ int start = 0; int end = s.length() - 1; while (start<= end) { if(s[start] != s[end]) return false; start++; end--; } return true;}bool SortByLength(string &s1,string &s2){ return s1.length()<s2.length();}vector<string> LongestPalindromicSubStr(string s){ vector<string> tmp; vector<string> ret; for(int i = 0;i<s.length();i++) { for (int j = 0;j<s.length(); j++) { string sub = s.substr(i,j-i + 1); if(IsPalindromicStr(sub)) { if(sub.length() % 2 == 1) tmp.push_back(sub); } } } sort(tmp.begin(),tmp.end(),SortByLength); int max = (tmp.end() - 1)->length(); for(int i = tmp.size() - 1; i>=0;i--) { if(tmp[i].length() == max) ret.push_back(tmp[i]); } return ret;}int main(int argc, const char * argv[]){ string s("mabcbatqabccba"); vector<string> ret = LongestPalindromicSubStr(s); for(int i = 0;i<ret.size();i++) { cout<<ret[i]<<endl; } return 0;}
#include <iostream>;#include <string>#include <vector>#include <algorithm>using namespace std;vector<string> DP_LongestOddPalindromicSubStr(const string &str){int strLen = str.length();vector<string> retVec;//长度为0的字符串都不符合要求if(strLen == 0)return retVec;//长度为0的字符串是长度为奇数的回文if(strLen == 1){retVec.push_back(str);return retVec;}bool **dp = new bool*[strLen];for(int i = 0;i < strLen;i++){dp[i] = new bool[strLen];;}for(int m = 0;m<strLen;m++)for(int n = 0;n<strLen;n++){if(m>=n){dp[m][n] = true;}else{dp[m][n] = false;}}int maxLen = 0;for(int k = 1;k<strLen;k++)for(int i = 0;i+k<strLen;i++){int j = i+k;if(str[i] != str[j]){dp[i][j] = false;}else{dp[i][j] = dp[i+1][j-1];if(dp[i][j]){if((k+1) % 2 == 1){retVec.push_back(str.substr(i,k+1));}}}}return retVec;}//仿函数,利用sort排序需要用到struct COMPARE{bool operator()(const string &a,const string &b){return a.length()<b.length();}};int main(){cout<<"Please enter a string"<<endl;string s;cin>>s;vector<string> retVec;retVec = DP_LongestOddPalindromicSubStr(s);sort(retVec.begin(),retVec.end(),COMPARE());vector<string> finalVec;int maxLen = retVec.rbegin()->length();for(vector<string>::reverse_iterator it =retVec.rbegin();it!= retVec.rend();it++){if(it->length() == maxLen){finalVec.push_back(*it);}else{break;}}return 0;}
关于C++,string中的substring方法:
string substr (size_t pos = 0, size_t len = npos) const;
- pos
- Position of the first character to be copied as a substring.If this is equal to the string length, the function returns an empty string.If this is greater than the string length, it throws out_of_range.Note: The first character is denoted by a value of 0 (not 1).
- len
- Number of characters to include in the substring (if the string is shorter, as many characters as possible are used).A value of string::npos indicates all characters until the end of the string.
0 0
- 找出一个字符串中的最长奇数回文。在控制台输入输出。
- 在一个字符串中,找出最长回文子串
- 找出一个字符串中最长的连续回文
- 找出一个字符串中最长回文子串
- 给定一个字符串s,找出最长的回文子串
- js实现找出字符串中的最长回文串
- 求一个字符串中的最长回文序列
- 求一个字符串中的最长回文前缀
- 找出字符串内最长的回文
- 找出字符串的最长回文 python实现
- 给定一个字符串,找出该字符串的最长回文子串
- 找出一个字符串中的最长palindrome子串
- 在一个字符串中查找最长回文子串
- 字符串中的最长回文字符
- 求字符串中的最长回文
- java 找出一个字符在 一个字符串中的位置
- 25.找出字符串中的最长数字串
- 找出字符串中的最长连续数字串
- IOS申请真机调试( xcode 5)详细解析
- Linux误操作嘟嘟声去掉方法
- 脑机接口:互联网遥远的疆界
- CvCreateImage函数说明
- C#窗体嵌入SetParent的用法
- 找出一个字符串中的最长奇数回文。在控制台输入输出。
- javascript为下拉列表添加数据项
- Qualcomm 高通芯片组与Android音频系统缺陷测评分析
- hadoop常见错误问题及解决方法(3)
- 51系列小型操作系统精髓 简单实现11 C语言版优化后说明(有图)
- 使用spring实现文件的上传
- js 开始时间,当前时间,结束时间的比较
- Android基础 -- Activity之间传递数据(bitmap和map对象)
- Oracle创建表分区----范围分区