| 59 | |
| 60 | template <typename TVal, typename TSequence, typename TResult> |
| 61 | size_t MakeLCS(TSequence s1, TSequence s2, TResult* res = nullptr, TLCSCtx<TVal>* ctx = nullptr) { |
| 62 | typedef TLCSCtx<TVal> TCtx; |
| 63 | |
| 64 | THolder<TCtx> ctxhld; |
| 65 | |
| 66 | if (!ctx) { |
| 67 | ctxhld.Reset(new TCtx()); |
| 68 | ctx = ctxhld.Get(); |
| 69 | } else { |
| 70 | ctx->Reset(); |
| 71 | } |
| 72 | |
| 73 | size_t maxsize = Max(s1.Size, s2.Size); |
| 74 | auto& index = *(ctx->Encounters); |
| 75 | |
| 76 | for (auto it = s1.Begin; it != s1.End; ++it) { |
| 77 | index[*it]; |
| 78 | } |
| 79 | |
| 80 | for (auto it = s2.Begin; it != s2.End; ++it) { |
| 81 | auto hit = index.find(*it); |
| 82 | |
| 83 | if (hit != index.end()) { |
| 84 | hit->second.push_back(it - s2.Begin); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (!res) { |
| 89 | auto& lastindex = ctx->ResultBuffer; |
| 90 | lastindex.reserve(maxsize); |
| 91 | |
| 92 | for (auto it1 = s1.Begin; it1 != s1.End; ++it1) { |
| 93 | const auto& sub2 = index[*it1]; |
| 94 | |
| 95 | for (auto it2 = sub2.rbegin(); it2 != sub2.rend(); ++it2) { |
| 96 | ui32 x = *it2; |
| 97 | |
| 98 | auto lit = LowerBound(lastindex.begin(), lastindex.end(), x); |
| 99 | |
| 100 | if (lit == lastindex.end()) { |
| 101 | lastindex.push_back(x); |
| 102 | } else { |
| 103 | *lit = x; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return lastindex.size(); |
| 109 | } else { |
| 110 | auto& lastindex = ctx->LastIndex; |
| 111 | auto& cover = ctx->Cover; |
| 112 | |
| 113 | lastindex.reserve(maxsize); |
| 114 | |
| 115 | for (auto it1 = s1.Begin; it1 != s1.End; ++it1) { |
| 116 | const auto& sub2 = index[*it1]; |
| 117 | |
| 118 | for (auto it2 = sub2.rbegin(); it2 != sub2.rend(); ++it2) { |
nothing calls this directly
no test coverage detected