백준25546 가채점

문제 링크

  • https://icpc.me/25546

사용 알고리즘

  • Suffix Array
  • Persistent Segment Tree

풀이

각 패턴 $P_i$에 대해, $S$에서 $P_i$가 $K_i$번째로 등장하는 위치를 구하는 문제입니다.
문자열 $T = S#P_1#P_2#\cdots#P_M$를 생각해 봅시다. 패턴 $P_i$가 등장하는 위치는 $T$의 접미사 배열 상에서 구간 형태로 나타낼 수 있고, LCP 배열과 RMQ를 이용해 파라메트릭 서치를 하면 각 패턴마다 $O(\log N)$ 시간에 구간을 구할 수 있습니다.

이 과정을 거치면 구간에서 $K_i$번째 수를 찾는 문제로 바뀌는데, 이건 PST를 사용하면 $O(\log N)$에 구할 수 있습니다.

전체 코드

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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<int> internal_sa_is(const vector<int> &s, int upper){
    int n = s.size(), m = 0;
    if(n == 0) return {};
    if(n == 1) return {0};
    if(n == 2) return s[0] < s[1] ? vector<int>{0, 1} : vector<int>{1, 0};

    vector<int> sa(n), ls(n), sum_l(upper+1), sum_s(upper+1);
    for(int i=n-2; i>=0; i--) ls[i] = s[i] != s[i+1] ? s[i] < s[i+1] : ls[i+1];
    for(int i=0; i<n; i++) (!ls[i] ? sum_s[s[i]] : sum_l[s[i]+1])++;
    for(int i=0; i<upper; i++) sum_s[i] += sum_l[i], sum_l[i+1] += sum_s[i];
    sum_s[upper] += sum_l[upper];

    auto induce = [&](const vector<int> &lms) -> void {
        fill(sa.begin(), sa.end(), -1);
        auto buf = sum_s;
        for(auto i : lms) if(i != n) sa[buf[s[i]]++] = i;
        buf = sum_l; sa[buf[s[n-1]]++] = n-1;
        for(int i=0; i<n; i++) if(sa[i] >= 1 && !ls[sa[i]-1]) sa[buf[s[sa[i]-1]]++] = sa[i]-1;
        buf = sum_l;
        for(int i=n-1; i>=0; i--) if(sa[i] >= 1 && ls[sa[i]-1]) sa[--buf[s[sa[i]-1]+1]] = sa[i]-1;
    };

    vector<int> lms_map(n+1, -1);
    for(int i=1; i<n; i++) if(!ls[i-1] && ls[i]) lms_map[i] = m++;
    std::vector<int> lms; lms.reserve(m);
    for(int i=1; i<n; i++) if(!ls[i-1] && ls[i]) lms.push_back(i);
    induce(lms);
    if(!m) return sa;

    int rec_upper = 0;
    vector<int> sorted_lms, rec_s(m); sorted_lms.reserve(m);
    for(const auto i : sa) if(lms_map[i] != -1) sorted_lms.push_back(i);
    rec_s[lms_map[sorted_lms[0]]] = 0;
    for(int i=1; i<m; i++){
        int l = sorted_lms[i-1], r = sorted_lms[i];
        int el = lms_map[l] + 1 < m ? lms[lms_map[l]+1] : n;
        int er = lms_map[r] + 1 < m ? lms[lms_map[r]+1] : n;
        bool flag = true;
        if(el - l != er - r) flag = false;
        else{
            for(; l<el; l++, r++) if(s[l] != s[r]) break;
            if(l == n || s[l] != s[r]) flag = false;
        }
        if(!flag) rec_upper++;
        rec_s[lms_map[sorted_lms[i]]] = rec_upper;
    }
    auto rec_sa = internal_sa_is(rec_s, rec_upper);
    for(int i=0; i<m; i++) sorted_lms[i] = lms[rec_sa[i]];
    induce(sorted_lms);
    return sa;
}

template<typename Container>
pair<vector<int>, vector<int>> SuffixArray(const Container &s){
    int n = s.size(), ch = 0;
    vector<int> idx(n), s2(n);
    iota(idx.begin(), idx.end(), 0);
    sort(idx.begin(), idx.end(), [&](int a, int b){ return s[a] < s[b]; });
    for(int i=0; i<n; i++) s2[idx[i]] = (ch += i && s[idx[i-1]] != s[idx[i]]);

    auto sa = internal_sa_is(s2, ch);
    vector<int> pos(n), lcp(n);
    for(int i=0; i<n; i++) pos[sa[i]] = i;
    for(int i=0, j=0; i<n; i++, j=max(j-1, 0)){
        if(!pos[i]) continue;
        while(sa[pos[i]-1]+j < n && sa[pos[i]]+j < n && s[sa[pos[i]-1]+j] == s[sa[pos[i]]+j]) j++;
        lcp[pos[i]] = j;
    }
    return { sa, lcp };
}

template<typename T> struct RMQ {
    vector<vector<T>> st;
    vector<int> lg;
    RMQ() = default;
    RMQ(const vector<T> &a){
        int n = a.size();
        st = vector<vector<T>>(__lg(n)+1, vector<T>(n));
        for(int i=0; i<n; i++) st[0][i] = a[i];
        for(int i=1; i<st.size(); i++) for(int j=0; j<n; j++) if(j + (1<<i) - 1 < n) st[i][j] = min(st[i-1][j], st[i-1][j+(1<<(i-1))]);
        lg.resize(n);
        for(int i=0; i<st.size(); i++) lg[1<<i] = i;
        for(int i=1; i<n; i++) if(!lg[i]) lg[i] = lg[i-1];
    }
    T query(int l, int r){
        if(l > r) return 0x3f3f3f3f;
        int k = lg[r-l+1];
        return min(st[k][l], st[k][r-(1<<k)+1]);
    }
};

constexpr int SZ = 1 << 21;
struct PST{
    struct Node{
        int l, r, v;
        Node(){ l = r = v = 0; }
    } T[SZ * 24];
    int Root[SZ*2], Count;
    PST(){ memset(Root, 0, sizeof Root); Count = 0; }
    void Init(int node, int s, int e){
        if(s == e) return;
        int m = (s + e) / 2;
        T[node].l = ++Count; T[node].r = ++Count;
        Init(T[node].l, s, m);
        Init(T[node].r, m+1, e);
    }
    void Update(int prv, int now, int s, int e, int x, int v){
        if(s == e){ T[now].v = prv ? T[prv].v + v : v; return; }
        int m = (s + e) / 2;
        if(x <= m){
            T[now].l = ++Count; T[now].r = T[prv].r;
            Update(T[prv].l, T[now].l, s, m, x, v);
        }
        else{
            T[now].r = ++Count; T[now].l = T[prv].l;
            Update(T[prv].r, T[now].r, m+1, e, x, v);
        }
        T[now].v = T[T[now].l].v + T[T[now].r].v;
    }
    int Query(int prv, int now, int s, int e, int k){
        if(s == e) return s;
        int diff = T[T[now].l].v - T[T[prv].l].v;
        int m = (s + e) / 2;
        if(k <= diff) return Query(T[prv].l, T[now].l, s, m, k);
        else return Query(T[prv].r, T[now].r, m+1, e, k-diff);
    }
} T;

int N, M; ll A, B[101010];
vector<string> V;
vector<int> S, C, CC;
vector<int> L, R;
vector<int> Pos;
vector<vector<int>> Idx;
RMQ<int> Q;

int FindL(int suf, int len){
    int l = 0, r = suf;
    while(l < r){
        int m = (l + r) / 2;
        if(Q.query(m+1, suf) >= len) r = m;
        else l = m + 1;
    }
    return r;
}

int FindR(int suf, int len){
    int l = suf, r = S.size() - 1;
    while(l < r){
        int m = (l + r + 1) / 2;
        if(Q.query(suf+1, m) >= len) l = m;
        else r = m - 1;
    }
    return l;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    cin >> N >> M >> A; V.resize(M+1);
    cin >> V[0];
    for(int i=1,t; i<=M; i++) cin >> t >> B[i] >> V[i];

    for(int i=0; i<V.size(); i++){
        L.push_back(S.size());
        for(auto c : V[i]) S.push_back(c);
        R.push_back(S.size()-1);
        S.push_back(-i-1);
    }
    auto [SA,LCP] = SuffixArray(S);
    Pos.resize(S.size());
    for(int i=0; i<S.size(); i++) Pos[SA[i]] = i;
    Idx.resize(V.size());
    for(int i=0; i<V.size(); i++){
        for(int j=L[i]; j<=R[i]; j++) Idx[i].push_back(Pos[j]);
        sort(Idx[i].begin(), Idx[i].end());
    }

    Q = RMQ<int>(LCP);
    C = vector<int>(S.size(), -1);
    CC = vector<int>(S.size(), 0);
    for(int i=L[0]; i<=R[0]; i++) C[Pos[i]] = i, CC[Pos[i]] = 1;
    partial_sum(CC.begin(), CC.end(), CC.begin());

    int cnt = 0;
    T.Init(T.Root[0]=++T.Count, 0, SZ-1);
    for(int i=1; i<S.size(); i++){
        if(C[i] == -1){ T.Root[i] = T.Root[i-1]; continue; }
        T.Root[i] = ++T.Count;
        T.Update(T.Root[i-1], T.Root[i], 0, SZ-1, C[i], 1);
    }

    int res = 0x3f3f3f3f;
    for(int i=1; i<=M; i++){
        int now = Pos[L[i]];
        int le = FindL(now, V[i].size());
        int ri = FindR(now, V[i].size());
        ll k = (A + B[i] - 1) / B[i];
        if(CC[ri] - CC[le-1] < k) continue;
        res = min<int>(res, T.Query(T.Root[le-1], T.Root[ri], 0, SZ-1, k) + V[i].size());
    }
    if(res == 0x3f3f3f3f) cout << "Fail";
    else cout << res;
}