|
50 | 50 |
|
51 | 51 | <!-- 这里可写通用的实现逻辑 -->
|
52 | 52 |
|
| 53 | +**方法一:记忆化搜索** |
| 54 | + |
| 55 | +我们设计一个函数 $dfs(i, j, x)$ 表示字符串 $s$ 中下标范围 $[i, j]$ 内,且以字符 $x$ 结尾的最长“好的回文子序列”的长度。答案为 $dfs(0, n - 1, 26)$。 |
| 56 | + |
| 57 | +函数 $dfs(i, j, x)$ 的计算过程如下: |
| 58 | + |
| 59 | +- 如果 $i >= j$,则 $dfs(i, j, x) = 0$; |
| 60 | +- 如果 $s[i] = s[j]$,且 $s[i] \neq x$,那么 $dfs(i, j, x) = dfs(i + 1, j - 1, s[i]) + 2$; |
| 61 | +- 如果 $s[i] \neq s[j]$,那么 $dfs(i, j, x) = max(dfs(i + 1, j, x), dfs(i, j - 1, x))$。 |
| 62 | + |
| 63 | +过程中,我们可以使用记忆化搜索的方式,避免重复计算。 |
| 64 | + |
| 65 | +时间复杂度 $O(n^2 \times C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字符集大小。本题中 $C = 26$。 |
| 66 | + |
53 | 67 | <!-- tabs:start -->
|
54 | 68 |
|
55 | 69 | ### **Python3**
|
56 | 70 |
|
57 | 71 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
58 | 72 |
|
59 | 73 | ```python
|
60 |
| - |
| 74 | +class Solution: |
| 75 | + def longestPalindromeSubseq(self, s: str) -> int: |
| 76 | + @cache |
| 77 | + def dfs(i, j, x): |
| 78 | + if i >= j: |
| 79 | + return 0 |
| 80 | + if s[i] == s[j] and s[i] != x: |
| 81 | + return dfs(i + 1, j - 1, s[i]) + 2 |
| 82 | + return max(dfs(i + 1, j, x), dfs(i, j - 1, x)) |
| 83 | + |
| 84 | + ans = dfs(0, len(s) - 1, '') |
| 85 | + dfs.cache_clear() |
| 86 | + return ans |
61 | 87 | ```
|
62 | 88 |
|
63 | 89 | ### **Java**
|
64 | 90 |
|
65 | 91 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 92 |
|
67 | 93 | ```java
|
| 94 | +class Solution { |
| 95 | + private int[][][] f; |
| 96 | + private String s; |
| 97 | + |
| 98 | + public int longestPalindromeSubseq(String s) { |
| 99 | + int n = s.length(); |
| 100 | + this.s = s; |
| 101 | + f = new int[n][n][27]; |
| 102 | + for (var a : f) { |
| 103 | + for (var b : a) { |
| 104 | + Arrays.fill(b, -1); |
| 105 | + } |
| 106 | + } |
| 107 | + return dfs(0, n - 1, 26); |
| 108 | + } |
| 109 | + |
| 110 | + private int dfs(int i, int j, int x) { |
| 111 | + if (i >= j) { |
| 112 | + return 0; |
| 113 | + } |
| 114 | + if (f[i][j][x] != -1) { |
| 115 | + return f[i][j][x]; |
| 116 | + } |
| 117 | + int ans = 0; |
| 118 | + if (s.charAt(i) == s.charAt(j) && s.charAt(i) - 'a' != x) { |
| 119 | + ans = dfs(i + 1, j - 1, s.charAt(i) - 'a') + 2; |
| 120 | + } else { |
| 121 | + ans = Math.max(dfs(i + 1, j, x), dfs(i, j - 1, x)); |
| 122 | + } |
| 123 | + f[i][j][x] = ans; |
| 124 | + return ans; |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### **C++** |
| 130 | + |
| 131 | +```cpp |
| 132 | +class Solution { |
| 133 | +public: |
| 134 | + int f[251][251][27]; |
| 135 | + |
| 136 | + int longestPalindromeSubseq(string s) { |
| 137 | + int n = s.size(); |
| 138 | + memset(f, -1, sizeof f); |
| 139 | + function<int(int, int, int)> dfs = [&](int i, int j, int x) -> int { |
| 140 | + if (i >= j) return 0; |
| 141 | + if (f[i][j][x] != -1) return f[i][j][x]; |
| 142 | + int ans = 0; |
| 143 | + if (s[i] == s[j] && s[i] - 'a' != x) ans = dfs(i + 1, j - 1, s[i] - 'a') + 2; |
| 144 | + else ans = max(dfs(i + 1, j, x), dfs(i, j - 1, x)); |
| 145 | + f[i][j][x] = ans; |
| 146 | + return ans; |
| 147 | + }; |
| 148 | + return dfs(0, n - 1, 26); |
| 149 | + } |
| 150 | +}; |
| 151 | +``` |
68 | 152 |
|
| 153 | +### **Go** |
| 154 | + |
| 155 | +```go |
| 156 | +func longestPalindromeSubseq(s string) int { |
| 157 | + n := len(s) |
| 158 | + f := make([][][]int, n) |
| 159 | + for i := range f { |
| 160 | + f[i] = make([][]int, n) |
| 161 | + for j := range f[i] { |
| 162 | + f[i][j] = make([]int, 27) |
| 163 | + for k := range f[i][j] { |
| 164 | + f[i][j][k] = -1 |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + var dfs func(i, j, x int) int |
| 169 | + dfs = func(i, j, x int) int { |
| 170 | + if i >= j { |
| 171 | + return 0 |
| 172 | + } |
| 173 | + if f[i][j][x] != -1 { |
| 174 | + return f[i][j][x] |
| 175 | + } |
| 176 | + ans := 0 |
| 177 | + if s[i] == s[j] && int(s[i]-'a') != x { |
| 178 | + ans = dfs(i+1, j-1, int(s[i]-'a')) + 2 |
| 179 | + } else { |
| 180 | + ans = max(dfs(i+1, j, x), dfs(i, j-1, x)) |
| 181 | + } |
| 182 | + f[i][j][x] = ans |
| 183 | + return ans |
| 184 | + } |
| 185 | + return dfs(0, n-1, 26) |
| 186 | +} |
| 187 | + |
| 188 | +func max(a, b int) int { |
| 189 | + if a > b { |
| 190 | + return a |
| 191 | + } |
| 192 | + return b |
| 193 | +} |
69 | 194 | ```
|
70 | 195 |
|
71 | 196 | ### **...**
|
|
0 commit comments