最长回文子串(只限小写字母)

#include<iostream>

#include<vector>

#include <algorithm>

#include<stack>

#include<map>

#include<cstring>

using namespace std;

class Solution {
public:
	//回文判断
    bool isParlindroom(string s){
        int low=0,high=s.size()-1;
        while(low<high){
            if(s[low]==s[high]){
                low++;
                high--;
            }
            else
                return false;
        }
        return true;
    }

    string longestPalindrome(string s) {
        vector<vector<int>> charsIndex(26,vector<int>(10,-1));
        int maxlength=0;
        string maxstring="";
        for(int i=0;i<s.size();i++){
            int a=s[i]-'a';
            //cout<<a<<endl;
            if(charsIndex[a][0]==-1)
                {
                    charsIndex[a][0]=i;
                    continue;
                }
            //查找相同字符下标
            int j=0;
            for(;j<charsIndex[a].size()&&charsIndex[a][j]!=-1;j++){
                //cout<<j<<"  "<<charsIndex[a][j];
                if(isParlindroom(s.substr(charsIndex[a][j],i+1-charsIndex[a][j]))){
                    //cout<<s.substr(charsIndex[a][j],i+1-charsIndex[a][j]);
                    if(maxlength<i+1-charsIndex[a][j]){
                        maxlength=i+1-charsIndex[a][j];
                        maxstring=s.substr(charsIndex[a][j],i+1-charsIndex[a][j]);

                    }
                }

            }
           charsIndex[a][j]=i;
        }

        if(maxstring=="")
            return maxstring+s[0];
        return maxstring;
    }
};

int main()

{
    string s;
    cin>>s;
    string res=Solution().longestPalindrome(s);
        cout<<res;


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值