I just know how to find LCS use dynamic programming:
if (s1 [i] == s2 [j]) dp [i] [j] = dp [i-1] [j-1] +1; else dp [i] [j] = max (dp [i-1] [j], dp [i] [j-1]).
But I have trouble with problem: Find LCS with s1 <= 1e6 and s2 > 1e3. What is the best way to find LCS? Thanks for help!!!
Auto comment: topic has been updated by wonderful_trip (previous revision, new revision, compare).
there is some algorithm that uses less time and memory but it is very uncomfortable to use.
http://en.wikipedia.org/wiki/Hirschberg's_algorithm try this
Can you explain more to me?
No, I don't know that algorithm -_("-")_-
(ᵟຶ︵ ᵟຶ)
Hirschberg's won't help here, Hirschberg's trades time for space. You get $$$\mathcal{O}(\min(n, m))$$$ space but still $$$\mathcal{O}(nm)$$$ time. I think that won't pass.
Meanwhile, what is the upper limit for $$$s_2$$$, did you mistype $$$>$$$ instead of $$$\le$$$?
This problem http://cnpt.edu.vn/problem/dplcs12 I make no mistype, but if s2 <= 1e3 and s1 <= 1e6, what can I do to solve it?
-deleted-
Source of the problem?
http://cnpt.edu.vn/problem/dplcs12 This is a link of problem.
If one of the strings is small, then the answer must also be small. In this case, let's try to swap the dimensions of our dp around. Try fixing the prefix of the smaller string and the length of the answer.
Assume $$$s1$$$ is the smaller string and $$$s2$$$ is the larger one. Compute $$$dp(i, lcs)$$$ = smallest prefix $$$j$$$ of $$$s2$$$ that is required to get a common subsequence of length $$$lcs$$$ using the prefix of length $$$i$$$ in $$$s1$$$. Similar to original LCS, you can decide to not match $$$s1[i]$$$ or to match it. If you decide to match it, it makes sense to match it with the next occurence of $$$s1[i]$$$ after $$$j$$$. So you need some extra bookkeeping to know where to jump to.
I think a solution using bit operations will work.