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 | 3985 |
2 | jiangly | 3741 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3489 |
7 | Radewoosh | 3483 |
8 | Kevin114514 | 3442 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
2 | atcoder_official | 162 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | nor | 150 |
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"