I need your help given string A, B. How to find longest subsequence of A that doesn't include B as substring ?
# | User | Rating |
---|---|---|
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 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
I need your help given string A, B. How to find longest subsequence of A that doesn't include B as substring ?
Name |
---|
Same problem https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/
What are the constraints of the problem?
If you're okay with an n*m solution (where n is the length of A and m is the length of B) then you can have a dp where dp[pos][matched] is a state where you're at some character in A and you've matched "matched" characters of B so far. So when processing a character of A (we'll call it c), there are two situations:
c is equal to the next character in B (B[matched]) and we go to state dp[pos+1][matched+1] or it doesn't match the next character and we go to state dp[pos+1][biggestMatchedPrefix] (see despotovski01's comment) and add 1 to the returned value (since we're increasing the subsequence size by 1). And of course if "matched" is ever equal to the length of B, then this is invalid since we've matched all of B (thus it's a substring of this subsequence)
Or we choose to not take that character and go to state dp[pos+1][matched] and add 0 because we aren't increasing the subsequence size.
If you're looking for a faster solution, I can't really think of one at the moment.
You've made a little mistake, when c doesn't match the next character, we don't necessarily go to 0 matched characters, but to the longest prefix of B that is also the suffix of B[matched] + c. Think like the KMP failure function.
Right I thought I was forgetting something. I think this is actually just very similar to this problem. Thanks for the correction!
this problem statement want to find LCS between A and B that doesn't include C as substring.
i want to make sure .. is it the same as to find D = LCS between A and B. and then find longest subsequence of D that doesn't include C
That's actually not the case. Consider this test case:
A = viviviaba
B = abavivivi
C = vi
D = vivivi
But the longest subsequence of D that doesn't contain C is 2 ("iv"). But if you instead took the subequence "aba" from A and B, you could get an answer of 3.
now the idea is very clear, thanks a lot Ahmad :)
This problem can be solved in O(|A| * |B|) complexity. The main idea of the solution includes dynamic programming.
We have two parameters in our dp states. First one is obviously the position of string A we are currently at, the second one is the length of prefix of string B which has been matched with the current subsequence. Let's denote these parameters as X and Y respectively.
Now, at position X we can either include this in our subsequence or skip it. If we skip it, our next call will be dp(X + 1, Y).
Now come to the inclusion part. There are two cases.
First Case: If our character at position X of string A matches with the character at position Y of string B, then our call will be 1 + dp(X + 1, Y + 1). Basically we increment each parameters by one as it matched.
Second Case: If our character at those positions do not match, we can not simply call dp(X + 1, 0). Because there is a possibility that some suffix of our subsequence matches with the prefix of string B. Check this part in following code and try to understand.
The F array gives us the length of the prefix which is also a suffix at that position. KMP was used to do this in O(N).