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
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
9 | awoo | 153 |
10 | luogu_official | 150 |
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
Название |
---|
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 :)