-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm5.cpp
38 lines (36 loc) · 1010 Bytes
/
tm5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// Created by dgy on 2024/4/21.
//
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
int maxLen = 0;
int left = 0;
int right = 0;
vector<vector<bool>> dp(s.size(), vector<bool>(s.size(), false));
for (int i = s.size() - 1; i >= 0; --i) {
for (int j = i; j < s.size(); ++j) {
if (s[i] == s[j]) {
if (j == i || j == i + 1) {
dp[i][j] = true;
} else if (dp[i + 1][j - 1]) {
dp[i][j] = true;
}
}
if (dp[i][j] && j - i + 1 > maxLen) {
maxLen = j - i + 1;
left = i;
right = j;
}
}
}
return s.substr(left, right - left + 1);
}
};
int main() {
Solution solution;
string s = "babad";
cout << solution.longestPalindrome(s);
}