I am not getting idea on the problem of dynamic programming. I am posting the link of question. Someone please help. Thnaks in advamce.Here is the link
# | 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 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
I am not getting idea on the problem of dynamic programming. I am posting the link of question. Someone please help. Thnaks in advamce.Here is the link
Name |
---|
Here is my idea for O(N2) dynamic programming solution (got AC):
Let our state be:
F[L][R] = the minimum amount of characters to insert to make the substring SLSL + 1...SR a palindrome.
Obviously the answer would be F[1][n]. Now let's come up with a recursive formula:
Suppose that we are trying to find the answer for some F[L][R]. We can see that at least one of the following three cases will always be true for any solution:
1) We add a character to the end to match SL — in that case, after that one move, we end up trying to make SL + 1...SR a palindrome. So the outcome of this case is F[L + 1][R] + 1.
2) We add a character to the beginning to match SR — in that case, after that one move, we end up trying to make SL...SR - 1 a palindrome. So the outcome of this case is F[L][R - 1] + 1
3) We match SL with SR. This is only possible if they are equal and in that case we have the outcome of F[L + 1][R - 1]
This gives us a simple formula:
F[L][R] = MIN(F[L + 1][R] + 1, F[L][R - 1] + 1) for SL ≠ SR
F[L][R] = MIN(F[L + 1][R] + 1, F[L][R - 1] + 1, F[L + 1][R - 1]) for SL = SR
Base cases are for R ≤ L, F[L][R] = 0
The complexity is O(N2) for both time and memory. Hope that helps :)