题解 | 密码截取
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
#include <iostream>
#include <string>
using namespace std;
// 检查字符串是否是回文
bool isPalindrome(const string &s) {
int start = 0;
int end = s.size() - 1;
while (start < end) {
if (s[start] != s[end]) {
return false;
}
start++;
end--;
}
return true;
}
int main() {
string s;
cin >> s;
int n = s.length();
int maxLength = 0;
// 检查每个可能的子串
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
string substring = s.substr(i, j - i + 1);
if (isPalindrome(substring)) {
maxLength = max(maxLength, j - i + 1);
}
}
}
cout << maxLength << endl;
return 0;
}
#牛客春招刷题训练营#
查看18道真题和解析
科大讯飞公司氛围 423人发布