题解 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
#include <bits/stdc++.h>
using namespace std;
bool isHuiWen(string s){
//cout << s << endl;
/*int left = 0, right = s.size() - 1;
while(left < right){
if(s[left] == s[right]){
left++;
right--;
}
else{
return false;
}
}
return true;*/
string tmp = s;
reverse(tmp.begin(), tmp.end());
if(tmp == s) return true;
else return false;
}
int main(){
string str = "";
while(cin >> str){
int res = INT_MIN;
int right = 0;
for(int i = 0; i < str.size(); i++){
right = i;
while(right < str.size()){
string tmp = str.substr(i, right - i + 1);
if(isHuiWen(tmp)){
res = max(res, right - i + 1);
}
right++;
}
}
cout << res << endl;
}
return 0;
}
华为题库题解 文章被收录于专栏
牛客华为题库的题解