欢迎使用CSDN-markdown编辑器

来源:互联网 发布:数据信息化管理 编辑:程序博客网 时间:2024/06/16 15:53
#include <iostream>#include <string>#include <string.h>using namespace std;bool isPalindrome(string str_input){    int pos_start = 0;    int pos_end = str_input.size() - 1;    while(pos_start <= pos_end){        if (str_input[pos_start] != str_input[pos_end])            return false;        pos_start++;        pos_end--;    }    return true;}//The first Methodstring findLongestPalindrome1(string str_input){    int i,j;    int len = str_input.size();    int pos_end = 1;    for(i=0;i<len;i++){        if (isPalindrome(str_input.substr(0,i+1))&&(i+1>pos_end))            pos_end = i+1;    }    string result = str_input;    while(pos_end<len){        result = str_input.substr(pos_end,1) + result;        pos_end++;}    return result;}//The Second Methodstring findLongestPalindrome2(string str_input){    int len_str = str_input.size();    bool dp[len_str][len_str];    memset(dp,false,len_str*len_str*sizeof(bool));    int i,j;    int pos_end = 0;    for(i=0;i<len_str;i++){        dp[i][i] = true;        if(i>=1&&str_input[i-1]==str_input[i])            dp[i-1][i] = true;}    for(int len = 2;len<len_str;len++){        for(i=len;i<len_str;i++){            if (dp[i-len+1][i-1] && str_input[i-len] == str_input[i])                dp[i-len][i] = true;}}    for(pos_end=len_str-1;(pos_end>=0)&&(!dp[0][pos_end]);pos_end--);    //cout<<pos_start<<'\t'<<pos_end<<endl;    pos_end++;    string result = str_input;    while(pos_end < len_str){        result = str_input.substr(pos_end,1)+ result;        pos_end++;    }    return result;}//The third Method:分奇偶进行判断string findLongestPalindrome3(string str_input){    int len_str = str_input.size();    int i,pos_start,pos_end;    int lenth_max = 0;    int pos_start_final;    //对于奇数的情况    for(i=0;i<len_str;i++){        pos_start=i-1;pos_end=i+1;        while(pos_start>=0&&pos_end<len_str&&str_input[pos_start]==str_input[pos_end]){            if(pos_end-pos_start>lenth_max){                lenth_max = pos_end-pos_start;                pos_start_final = pos_start;}            pos_start--;pos_end++;}}    //对于偶数的情况    for(i=0;i<len_str;i++){        pos_start = i;pos_end = i+1;        while(pos_start>=0&&pos_end<len_str&&str_input[pos_start]==str_input[pos_end]){            if(pos_end-pos_start>lenth_max){                lenth_max = pos_end-pos_start;                pos_start_final = pos_start;}            pos_end++;pos_start--;}}    return str_input.substr(pos_start_final,lenth_max+1);}//The Fouth Methodstring findLongestPalindrome4(string str_input){    int str_len = str_input.size();    int i;    for(i=0;i<str_len-1;i++)        str_input.insert(2*i+1,"#");    str_len = str_len + str_len - 1;    int pos_start,pos_end;    int max_length = 0;    int pos_start_final = 0;    for(i=0;i<str_len;i++){        pos_start=i-1;pos_end=i+1;        while(pos_start>=0&&pos_end<=str_len&&str_input[pos_start]==str_input[pos_end]){            if(pos_end-pos_start>max_length){                max_length = pos_end-pos_start;                pos_start_final = pos_start;}            pos_end++;pos_start--;        }    }    string result = "";    for(i=0;i<=max_length;i++){        if (str_input[i+pos_start_final] != '#')            result = result + str_input[i+pos_start_final];}    return result;}int main(){    string str_input = "32112345";    cout<<"result_before:"<<str_input<<endl;    string result = findLongestPalindrome4(str_input);    cout<<"result__after:"<<result<<endl;    return 0;}
0 0