码迷,mamicode.com
首页 > 其他好文 > 详细

[string]Longest Palindromic Substring

时间:2015-12-07 22:48:22      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

Total Accepted: 82026 Total Submissions: 379898 Difficulty: Medium

 

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

 

Subscribe to see which companies asked this question

Show Tags
 

1.动态规划

class Solution {
public:
    string longestPalindrome(string s) {
        int sSize = s.size();
        if(sSize <=1){
            return s;
        }
        int start = 0;
        int maxLen = 1;
        bool help[1000][1000] = {false};
        for(int i=0;i<sSize;i++){
            help[i][i] = true;
            if(i>0 && s[i-1]==s[i]){
                help[i-1][i] = true;
                start = i-1;
                maxLen = 2;
            }
        }
        for(int len = 3;len<=sSize;len++){
            for(int i=0;i<=sSize-len;i++){
                int j = i+len-1;
                if(help[i+1][j-1] && s[i]==s[j]){
                    help[i][j] = true;
                    start = i;
                    maxLen = len;
                }
            }
        }
        return s.substr(start,maxLen);
    }
};

2.中心扩展

3.manacher

[string]Longest Palindromic Substring

标签:

原文地址:http://www.cnblogs.com/zengzy/p/5027637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!