aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lcs/lcs_via_lis.h
blob: c652c31c81ae8fd4d8686b12b95f25c77105600c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#pragma once

#include <library/cpp/containers/paged_vector/paged_vector.h>

#include <util/generic/ptr.h>
#include <util/generic/hash.h>
#include <util/generic/vector.h>
#include <util/generic/algorithm.h>
#include <util/memory/pool.h>

namespace NLCS {
    template <typename TVal>
    struct TLCSCtx {
        typedef TVector<ui32> TSubsequence; 
        typedef THashMap<TVal, TSubsequence, THash<TVal>, TEqualTo<TVal>, ::TPoolAllocator> TEncounterIndex; 
        typedef TVector<std::pair<ui32, ui32>> TLastIndex; 
        typedef NPagedVector::TPagedVector<TSubsequence, 4096> TCover;

        TMemoryPool Pool;
        THolder<TEncounterIndex> Encounters;
        TLastIndex LastIndex;
        TCover Cover;

        TSubsequence ResultBuffer;

        TLCSCtx()
            : Pool(16 * 1024 - 64, TMemoryPool::TExpGrow::Instance())
        {
            Reset();
        }

        void Reset() {
            Encounters.Reset(nullptr);
            Pool.Clear();
            Encounters.Reset(new TEncounterIndex(&Pool));
            LastIndex.clear();
            Cover.clear();
            ResultBuffer.clear();
        }
    };

    namespace NPrivate {
        template <typename TIt, typename TVl>
        struct TSequence {
            typedef TIt TIter;
            typedef TVl TVal;

            const TIter Begin;
            const TIter End;
            const size_t Size;

            TSequence(TIter beg, TIter end)
                : Begin(beg)
                , End(end)
                , Size(end - beg)
            {
            }
        };

        template <typename TVal, typename TSequence, typename TResult>
        size_t MakeLCS(TSequence s1, TSequence s2, TResult* res = nullptr, TLCSCtx<TVal>* ctx = nullptr) {
            typedef TLCSCtx<TVal> TCtx;

            THolder<TCtx> ctxhld;

            if (!ctx) {
                ctxhld.Reset(new TCtx());
                ctx = ctxhld.Get();
            } else {
                ctx->Reset();
            }

            size_t maxsize = Max(s1.Size, s2.Size);
            auto& index = *(ctx->Encounters);

            for (auto it = s1.Begin; it != s1.End; ++it) {
                index[*it];
            }

            for (auto it = s2.Begin; it != s2.End; ++it) {
                auto hit = index.find(*it);

                if (hit != index.end()) {
                    hit->second.push_back(it - s2.Begin);
                }
            }

            if (!res) {
                auto& lastindex = ctx->ResultBuffer;
                lastindex.reserve(maxsize);

                for (auto it1 = s1.Begin; it1 != s1.End; ++it1) {
                    const auto& sub2 = index[*it1];

                    for (auto it2 = sub2.rbegin(); it2 != sub2.rend(); ++it2) {
                        ui32 x = *it2;

                        auto lit = LowerBound(lastindex.begin(), lastindex.end(), x);

                        if (lit == lastindex.end()) {
                            lastindex.push_back(x);
                        } else {
                            *lit = x;
                        }
                    }
                }

                return lastindex.size();
            } else {
                auto& lastindex = ctx->LastIndex;
                auto& cover = ctx->Cover;

                lastindex.reserve(maxsize);

                for (auto it1 = s1.Begin; it1 != s1.End; ++it1) {
                    const auto& sub2 = index[*it1];

                    for (auto it2 = sub2.rbegin(); it2 != sub2.rend(); ++it2) {
                        ui32 x = *it2;

                        auto lit = LowerBound(lastindex.begin(), lastindex.end(), std::make_pair(x, (ui32)0u));

                        if (lit == lastindex.end()) {
                            lastindex.push_back(std::make_pair(x, cover.size()));
                            cover.emplace_back();
                            cover.back().push_back(x);
                        } else {
                            *lit = std::make_pair(x, lit->second);
                            cover[lit->second].push_back(x);
                        }
                    }
                }

                if (cover.empty()) {
                    return 0;
                }

                std::reverse(cover.begin(), cover.end());

                auto& resbuf = ctx->ResultBuffer;

                resbuf.push_back(cover.front().front());

                for (auto it = cover.begin() + 1; it != cover.end(); ++it) {
                    auto pit = UpperBound(it->begin(), it->end(), resbuf.back(), std::greater<ui32>());

                    Y_VERIFY(pit != it->end(), " ");

                    resbuf.push_back(*pit);
                }

                std::reverse(resbuf.begin(), resbuf.end());

                for (auto it = resbuf.begin(); it != resbuf.end(); ++it) {
                    res->push_back(*(s2.Begin + *it));
                }

                return lastindex.size();
            }
        }
    }

    template <typename TVal, typename TIter, typename TResult>
    size_t MakeLCS(TIter beg1, TIter end1, TIter beg2, TIter end2, TResult* res = nullptr, TLCSCtx<TVal>* ctx = nullptr) {
        typedef NPrivate::TSequence<TIter, TVal> TSeq;

        size_t sz1 = end1 - beg1;
        size_t sz2 = end2 - beg2;

        if (sz2 > sz1) {
            DoSwap(beg1, beg2);
            DoSwap(end1, end2);
            DoSwap(sz1, sz2);
        }

        return NPrivate::MakeLCS<TVal>(TSeq(beg1, end1), TSeq(beg2, end2), res, ctx);
    }

    template <typename TVal, typename TColl, typename TRes>
    size_t MakeLCS(const TColl& coll1, const TColl& coll2, TRes* res = nullptr, TLCSCtx<TVal>* ctx = nullptr) {
        return MakeLCS<TVal>(coll1.begin(), coll1.end(), coll2.begin(), coll2.end(), res, ctx);
    }

    template <typename TVal, typename TIter>
    size_t MeasureLCS(TIter beg1, TIter end1, TIter beg2, TIter end2, TLCSCtx<TVal>* ctx = nullptr) {
        return MakeLCS<TVal>(beg1, end1, beg2, end2, (TVector<TVal>*)nullptr, ctx); 
    }

    template <typename TVal, typename TColl>
    size_t MeasureLCS(const TColl& coll1, const TColl& coll2, TLCSCtx<TVal>* ctx = nullptr) {
        return MeasureLCS<TVal>(coll1.begin(), coll1.end(), coll2.begin(), coll2.end(), ctx);
    }
}