forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain3.cpp
46 lines (35 loc) · 1.07 KB
/
main3.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
39
40
41
42
43
44
45
46
/// Source : https://leetcode.com/problems/longest-palindromic-substring/
/// Author : liuyubobobo
/// Time : 2019-01-11
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Memory Search
/// State: is s.substr(start, len) a palindrome?
///
/// Time Complexity: O(n^2)
/// Space Complexity: O(n^2)
class Solution {
public:
string longestPalindrome(string s) {
if(s == "") return s;
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n + 1, -1)); // start, len
for(int len = n; len >= 1; len --)
for(int start = 0; start + len - 1 < n; start ++)
if(dfs(s, start, len, dp))
return s.substr(start, len);
return "";
}
private:
int dfs(const string& s, int start, int len, vector<vector<int>>& dp){
if(len <= 1) return dp[start][len] = 1;
if(dp[start][len] >= 0) return dp[start][len];
return dp[start][len] =
(s[start] == s[start + len - 1] && dfs(s, start + 1, len - 2, dp));
}
};
int main() {
return 0;
}