首页 > 其他 > 详细

[string]Longest Palindromic Substring

时间:2015-12-07 22:45:28      阅读:200      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!