题目链接 5.最长回文子串
题目描述: 解题思路:(来自力扣大神解析) 代码:
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
if (n < 2) {
return s;
}
int maxLen = 1;
int begin = 0;
bool dp[n][n];
for (int i = 0; i < n; i++) {
dp[i][i] = true;
}
for (int L = 2; L <= n; L++) {
for (int i = 0; i < n; i++) {
int j = L + i - 1;
if (j >= n) {
break;
}
if (s[i] != s[j]) {
dp[i][j] = false;
} else {
if (j - i < 3) {
dp[i][j] = true;
} else {
dp[i][j] = dp[i + 1][j - 1];
}
}
if (dp[i][j] && j - i + 1 > maxLen) {
maxLen = j - i + 1;
begin = i;
}
}
}
return s.substr(begin, maxLen);
}
};
int main()
{
Solution s;
string sc;
cin >> sc;
cout << s.longestPalindrome(sc);
return 0;
}
|