标签:int string lin nbsp tin ret ++ dex code
关于string类的substr(start,length);
有些资料是substr(startIndex,endIndex);
通过的代码如下:
string longestPalindrome(string& s) {
// Write your code here
int alength = 1, blength = 0,start=-1;
int length = 0 ;
int slength=s.length();
for ( int i = 0;i < slength;i++) {
for (int j = i, m = j + 1, n = j - 1;(m < slength) && (n >= 0) && (s[m] == s[n]);m++, n--) {//对于“aca”类型的输入,得到回文子串的长度;
alength+=2;
}
for (int j = i, m = j+1, n = j;(m<slength)&&(n>=0)&&(s[m]==s[n]);m++, n--) {//对于“aa”类型的输入,得到回文子串的长度;
blength+=2;
}
if (alength > length) {
length = alength;
start = i-alength/2;//对于“aca”类型的输入,得到回文子串起始索引;
}
if (blength>length) {
length = blength;
start=i+1-blength/2;//对于“aca”类型的输入,得到回文子串起始索引;
}
alength = 1;
blength = 0;
}
int end = start + length;
string str;
for (int i = start;i < end;i++) {
str += s[i];
}
return str;
//return s.substr(start,length);
}
标签:int string lin nbsp tin ret ++ dex code
原文地址:http://www.cnblogs.com/lifan323/p/6492998.html