How to solve this problem — http://mirror.codeforces.com/problemset/problem/346/B ? I am not able to understand much by reading the editorial. Please Help!!
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
How to solve this problem — http://mirror.codeforces.com/problemset/problem/346/B ? I am not able to understand much by reading the editorial. Please Help!!
Name |
---|
Auto comment: topic has been updated by viralm (previous revision, new revision, compare).
Anyone?
You have 3 states:
dp[position on the first string][position on the second string][where you are on the virus]
dp[i][j][k]
if k == virus.size() return -INF, if i == first.size() || j==second.size() return 0
then you take the maximum between
dp[i+1][j][k], dp[i][j+1][k] and if first[i]==second[j] you can take it and and you'll have 1 + dp[i+1][j+1][next_k]. You need to use KMP to know what's the next_k depending on the letter you are taking. Make an array that will remember which choice you took on each step and you can run through the memoization to get the answer.
In order to use kmp, we need to keep track of the LCS found upto a particular state. How do you suggest to do that? Should I store the LCS itself in another array[][][] ?
No you don't need to store it. The next state of the KMP depends on:
Next letter (on the dp you know what's the next letter)
Where you are on the virus (that's the k)
And nothing more
Notice that the optimal subsequence will be a subsequence of the longest common subsequence. Then, it is a good idea to obtain the longest common subsequence using Needleman-Wunsch (use cost for mismatch equal to infinity and deletions/insertions cost 0) or other equivalent algorithm. Once you have this LCS, use a longest increasing subsequence-like approach in order to know the answer.
That's not true:
"aaab" and "abaa", the LCS is "aaa". If the virus is "a", the answer is actually "b"