I need your help given string A, B. How to find longest subsequence of A that doesn't include B as substring ?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
I need your help given string A, B. How to find longest subsequence of A that doesn't include B as substring ?
Название |
---|
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).