找出一个字符串中的最长奇数回文。在控制台输入输出。

来源:互联网 发布: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.
size_t is an unsigned integral type.

0 0
原创粉丝点击