I am unable to find where my code is going wrong.First i calculated LCS of string and it's reverse than printed ans=length of string — LCS.
Can anyone tell me where i am wrong . my code is : here
I am unable to find where my code is going wrong.First i calculated LCS of string and it's reverse than printed ans=length of string — LCS.
Can anyone tell me where i am wrong . my code is : here
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | jiangly | 3631 |
| 4 | Kevin114514 | 3574 |
| 5 | maroonrk | 3521 |
| 6 | strapple | 3515 |
| 7 | Radewoosh | 3461 |
| 8 | tourist | 3428 |
| 9 | turmax | 3378 |
| 10 | Um_nik | 3376 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 161 |
| 2 | adamant | 146 |
| 3 | Um_nik | 145 |
| 4 | Dominater069 | 142 |
| 5 | errorgorn | 140 |
| 6 | cry | 138 |
| 7 | Proof_by_QED | 136 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 10 | soullless | 133 |
| Название |
|---|



I think that you should initialize dp with zeroes and then take care of this:
If the last two elements are different, it doesn't mean that the length of the LCS is 0.
I'm not sure if I understood your code right. Why don't you just take the reversed string and use 2D DP or just DP[from][to] without computing the LCS:
Let dp[i][j] gives us the answer for the sequence S[i],S[i+1],...,S[j].
Then you have two cases:
If S[i]==S[j], then dp[i][j]=dp[i+1][j-1].
If S[i]!=S[j], then dp[i][j]=1+min(dp[i+1][j],dp[i][j-1]).
Your answer is correct i had implemented that but it gives tle because of space complexity. I searched for solution and got an answer that space optimization is required for this problem.That is why i was trying to write code like that but gave wrong answer. Can anyone help??? my code is :Your text to link here...
I just got accepted.
My first idea was the one that I shared with you yesterday — dp[from][to] but it received TLE:
ioipalin1
My second idea was actually the same but my dp looks like dp[i][from], not dp[from][to] because this can be easily space optimized. However, this second idea got AC(I don't know why):
ioipalin2
But I made an optimization and it got AC with linear space:
ioipalin3
Can you please explain how you have optimized to get AC.
I am not getting how to optimize space using dp[from][to]......
You asked me how to optimize (from,to) with PM. I am using my phone now and I can't write a PM. Those who have tried will know. So my answer goes here:
Actually I don't even know if it is possible to optimize it and that is why I transformed dp[from][to] to dp[length][from] where length=to-from. Then to calculate the state (length,from) we need only the states with length-1 and length-2. Check my second and third posted codes to understand it completely.
Optimise space. You can see that in LCS :
dp[i][j] = max(dp[i-1][j],dp[i][j-1]) for non equal a[i] and b[j]
dp[i][j] = dp[i-1][j-1] for equal a[i] and b[j]
Now you can observe that for a state(i,j) you need information of only two rows, i and i-1. This can help you reduce your state drastically. Create an array of 2 * N instead of N * N.