結果

問題 No.1018 suffixsuffixsuffix
ユーザー potoooooooopotoooooooo
提出日時 2020-04-03 23:25:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 2,654 bytes
コンパイル時間 1,848 ms
コンパイル使用メモリ 175,036 KB
実行使用メモリ 6,600 KB
最終ジャッジ日時 2023-09-16 04:57:15
合計ジャッジ時間 7,902 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 171 ms
6,288 KB
testcase_10 AC 178 ms
6,596 KB
testcase_11 AC 184 ms
6,600 KB
testcase_12 AC 164 ms
6,284 KB
testcase_13 AC 166 ms
6,296 KB
testcase_14 AC 168 ms
6,328 KB
testcase_15 AC 171 ms
6,328 KB
testcase_16 AC 184 ms
6,584 KB
testcase_17 AC 161 ms
6,348 KB
testcase_18 AC 166 ms
6,340 KB
testcase_19 AC 21 ms
4,380 KB
testcase_20 AC 21 ms
4,376 KB
testcase_21 AC 21 ms
4,380 KB
testcase_22 AC 21 ms
4,376 KB
testcase_23 AC 22 ms
4,376 KB
testcase_24 AC 178 ms
6,304 KB
testcase_25 AC 190 ms
6,560 KB
testcase_26 AC 169 ms
6,340 KB
testcase_27 AC 166 ms
6,300 KB
testcase_28 AC 164 ms
6,352 KB
testcase_29 AC 26 ms
4,376 KB
testcase_30 AC 26 ms
4,380 KB
testcase_31 AC 26 ms
4,380 KB
testcase_32 AC 27 ms
4,380 KB
testcase_33 AC 26 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 22 ms
4,380 KB
testcase_36 AC 27 ms
4,376 KB
testcase_37 AC 185 ms
6,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<class T> struct SuffixArray {
    int N, K;
    vector<int> sa, rank, tmp;

    SuffixArray(int n, T &s) : N(n) {
        sa.resize(N+1);
        rank.resize(N+1);
        tmp.resize(N+1);
        for (int i = 0; i <= N; i++) {
            sa[i] = i;
            rank[i] = i < N ? s[i] : -1; 
        }

        auto compare = [&] (int i, int j) {
            if (rank[i] != rank[j]) return rank[i] < rank[j];
            else {
                int ri = i + K <= N ? rank[i + K] : -1;
                int rj = j + K <= N ? rank[j + K] : -1;
                return ri < rj;
            }
        };

        for (K = 1; K <= N; K <<= 1) {
            sort(sa.begin(), sa.end(), compare);
            tmp[sa[0]] = 0;
            for (int i = 1; i <= n; i++) {
                tmp[sa[i]] = tmp[sa[i-1]] + (compare(sa[i-1], sa[i]));
            }
            for (int i = 0; i <= n; i++) {
                rank[i] = tmp[i];
            }
        }
    }

    // return index of i-th suffix 
    int operator[] (const int &i) const { return sa[i+1]; }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    long long n, m, q; cin >> n >> m >> q;
    string s; cin >> s;
    auto cut = [&](int x) {
        bool ok = true;
        for (int i = 0; i < n-x && ok; i++) {
            if (s[i] == s[i+x]) continue;
            ok = false;
        }
        return ok;
    };
    long long t = n;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            if (cut(i)) t = min(i, t);
            if (cut(n/i)) t = min(n/i, t);
        }
    }
    s = s.substr(0, t);
    m *= n / t; n = t;
    string u = s + s;
    SuffixArray<string> sf(n*2, u);
    long long curr = 0; int i = 0;
    vector<long long> ans(q);
    for (int qi = 0; qi < q; qi++) {
        long long k; cin >> k;
        while (curr < k) {
            auto x = sf[i] + 1;
            //cout << qi << " " << curr << " " << k << " " << x << "\n";
            if (x > n) {
                if (curr + 1 < k) {
                    curr++, i++; continue;
                } else if (curr + 1 == k) {
                    ans[qi] = (m-1) * n + (x-n);
                    break;
                }
            } else {
                if (curr + (m-1) < k) {
                    curr += m-1, i++;
                } else {
                    auto y = k - curr;
                    ans[qi] = (m-y-1) * n + x;
                    break;
                }
            }
        }
    }
    for (int i = 0; i < q; i++) {
        if (i) cout << " ";
        cout << ans[i];
    }
    cout << "\n";
    return 0;
}
0