# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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)