首页 > 其他 > 详细

求字符串中对称的子字符串的最大长度

时间:2014-03-14 18:03:21      阅读:380      评论:0      收藏:0      [点我收藏+]

一般方法:

bubuko.com,布布扣
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//形如aba
int oneCenter(string const& str, int index){
    int len = 1 , i = 1;
    while((index-i)>=0 && (index+i)< str.length() &&  str[index+i]==str[index-i]){
        len += 2;
        i++;
    }
    return len;
}

//形如abba
int twoCenter(string const& str, int index){
    int len = 0, i = 0;
    while( (index - i) >= 0 && (index+1+i < str.length()) &&  str[index-i] == str[index+1+i]){
        len += 2;
        i ++;
    }
    return len;
}

int solve(string const& str){
    int maxLength = 0;
    for(int i = 0 ; i < str.length(); ++ i){
        maxLength = max(maxLength,max(oneCenter(str,i),twoCenter(str,i)));    
    }
    return maxLength;
}

int main(){
    string str;
    cin >>  str;
    cout<<"Max longest string lenght is  "
        <<solve(str)
        <<endl;
}
bubuko.com,布布扣

求字符串中对称的子字符串的最大长度,布布扣,bubuko.com

求字符串中对称的子字符串的最大长度

原文:http://www.cnblogs.com/xiongqiangcs/p/3599553.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!