标签:
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.
1 class Solution { 2 public: 3 string longestPalindrome(string s) { 4 5 int n=s.length(); 6 //采用vector会超时 7 //vector<vector<bool> > dp(n,vector<bool>(n)); 8 bool dp[1000][1000]={false}; 9 int maxDis=-1; 10 int start; 11 int end; 12 int dis; 13 for(int i=n-1;i>=0;i--) 14 { 15 for(int j=i;j<n;j++) 16 { 17 if(s[i]==s[j]) 18 { 19 dis=j-i; 20 if(dis==0||dis==1||dp[i+1][j-1]&&dis>1) 21 { 22 dp[i][j]=true; 23 if(dis>maxDis) 24 { 25 maxDis=dis; 26 start=i; 27 end=j; 28 } 29 } 30 } 31 } 32 } 33 return s.substr(start,end-start+1); 34 } 35 };
1 class Solution { 2 3 public: 4 string longestPalindrome(string s) { 5 6 int n=s.length(); 7 int id=0; 8 int right=0; 9 int i; 10 11 string pre_s; 12 pre_s="^#"; 13 for(int i=0;i<n;i++) 14 { 15 pre_s=pre_s+s[i]+‘#‘; 16 } 17 pre_s+=‘$‘; 18 19 20 int n1=pre_s.length(); 21 22 vector<int> p(n1); 23 24 for(i=1;i<n1-1;i++) 25 { 26 if(i<right) 27 { 28 p[i]=min(p[2*id-i],right-i); 29 } 30 else 31 { 32 p[i]=1; 33 } 34 35 while(pre_s[i+p[i]]==pre_s[i-p[i]]) 36 { 37 p[i]++; 38 } 39 40 if(right<p[i]+i) 41 { 42 right=p[i]+i; 43 id=i; 44 } 45 } 46 47 int index=0; 48 int maxLen=0; 49 for(i=0;i<n1-1;i++) 50 { 51 if(p[i]>maxLen) 52 { 53 maxLen=p[i]-1; 54 index=i; 55 } 56 } 57 //注意此处找到原来串的位置 58 return s.substr((index-maxLen-1)/2,(maxLen)); 59 60 } 61 };
L【leetcode】ongest Palindromic Substring
标签:
原文地址:http://www.cnblogs.com/reachteam/p/4190772.html