# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | nor | 150 |
Name |
---|
Maybe it's because the maximum length of the string is 25? Though you can solve this problem with dynamic programming in O(N3) with the same recurrence relation using memoization.
There are 2^26-2 = 67108862 states, and what you do in one state is linear. Can you explain why it's O(N^3)?
Let dp(i, j) be a boolean function that tells us if we can reduce the substring s[i][j]. Also define function l(i) and r(i), where l(i) the largest index j ≥ i such that s[i] = s[i + 1] = ... = s[j], and l(i) as the lowest left index with the mentioned property.
Now here's the code for the recurence:
(There are also some cornes cases)
We observe that if r(i) > i then we can transition to state dp(r(i) + 1, j) and see if we can reduce the substring s[r(i) + 1, j]. But also we can combine the substring s[i][r(i)] with some other substring s[x][y] if all their characters are equal. So we are left to reduce s[r(i) + 1][x - 1] and s[y + 1][j]. We use r(i) and l(x) because it's always better to pick these indeces instead of fixing the right point of an interval and try to extend it to the left until we get to a different character.
I know that I worded it pretty badly, but try to simulate the recurrence on the examples and it should become clear why this kind of approach works. (but take notice, I'm not 100% sure that this solution is correct)