标签:
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
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