5. Longest Palindromic Substring
Given a string s, find the longestpalindromic substring in s. You may assume that the maximum lengthof s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
//问题通过样例输入输出已经很明白了,如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string,如aba,或者abba。本题是这样的,给定输入一个字符串,要求输出一个子串,使得子串是最长的padromicstring。
【解法一】
So:下列解法是的时间复杂度为O(N*N*N)
vector
vector
map[a]=vec1;
map[b]=vec2.
这样,完成了第一步,构造好了一个map,储存了原字符串中,各个字符出现的位置了。
现在进行第二步,从头到尾再扫描一遍字符串,根据map中储存的位置,很容易找到候选子串了。
class Solution {
public:
string longestPalindrome(string s) {
int n=s.length();
if(n==0)return "";
if(n==1)return s;
string c_string="";
string l_string="";
int c_l=0,l_l=0;
map
for(int i=0;i
map
if(s_map_t==s_map.end())//没有查找到
{
vector
pos.push_back(i);
s_map.insert(pair
}else{
vector
pos.push_back(i);
}
}//以上构建了一个map
for(int index=0;index
map
if(s_map_t2->second.size()==1){
c_l=1;
c_string=s[index];
if(c_l>l_l){
l_l=c_l;
l_string=c_string;
}
}else{
vector
int it_num=it.size();
int head=index;
for(intj=(it_num-1);j>=0;--j){
head=index;
int t=it[j];
if(t
c_l=t-head+1;
if(c_l
c_string=substr(head,t);
while(((t-head)>=1)&&(c_string[head]==c_string[t])){
head++;
t--;
}
if((t-head)=-1||(t-head)==0){
l_l=c_l;
l_string=c_string;
}
}
}
}
return l_string;
}
};
遗留问题:Your answer
Line12: no matching function for call to'std::map
【解法二】动态规划DP(时间复杂度N*N)
首先,写出动态转移方程。
Define P[ i, j ] ← true iff the substring Si …Sj is a palindrome, otherwise false.
P[ i, j ] ← ( P[ i+1, j-1 ] and Si = Sj ) ,显然,如果一个子串是回文串,并且如果从它的左右两侧分别向外扩展的一位也相等,那么这个子串就可以从左右两侧分别向外扩展一位。
其中的base case是
P[ i, i ] ← true
P[ i, i+1 ] ← ( Si = Si+1 )
然后,看一个例子。
假设有个字符串是adade,现在要找到其中的最长回文子串。使用上面的动态转移方程,有如下的过程:
按照红箭头->黄箭头->蓝箭头->绿箭头->橙箭头的顺序依次填入矩阵,通过这个矩阵记录从i到j是否是一个回文串。
最后, 看code。
class Solution {
public:
string longestPalindrome(string s) {
int n=s.length();
int l=0;
int max=1;
bool table[1000][1000]={false};
for(int i=0;i
table[i][i]=true;
}
for(int i=0;i
if(s[i]==s[i+1]){
table[i][i+1]=true;
l=i;
max=2;
}
}
for(int len=3;len<=n;len++){
for(int i=0;i
int j=i+len-1;
if(table[i+1][j-1]&&s[i]==s[j]){
table[i][j]=true;
l=i;
max=len;
}
}
}
return s.substr(l,max);
}
};