玄关求调
  • 板块题目总版
  • 楼主tc291311
  • 当前回复4
  • 已保存回复5
  • 发布时间2025/2/4 11:47
  • 上次更新2025/2/4 17:22:35
查看原帖
玄关求调
1340395
tc291311楼主2025/2/4 11:47

P3805

CODE

#include <iostream>
#include <string>
using namespace std;
int expandAroundCenter(const string& s, int left, int right) {
    while (left >= 0 && right < s.length() && s[left] == s[right]) {
        left--;
        right++;
    }
    return right - left - 1;
}
int longestPalindromeSubstring(const string& s) {
    if (s.empty()) return 0;
    int maxLength = 1;
    for (int i = 0; i < s.length(); ++i) {
        int len1 = expandAroundCenter(s, i, i); 
        int len2 = expandAroundCenter(s, i, i + 1);
        maxLength = max(maxLength, max(len1, len2));
    }
    return maxLength;
}
int main() {
    string s;
    cin >> s;
    cout << longestPalindromeSubstring(s) << endl;
    return 0;
}

码风有点丑,勿喷

测评结果

2025/2/4 11:47
加载中...