結果

問題 No.1018 suffixsuffixsuffix
ユーザー hitonanodehitonanode
提出日時 2020-04-03 23:06:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,862 bytes
コンパイル時間 2,086 ms
コンパイル使用メモリ 188,808 KB
実行使用メモリ 46,292 KB
最終ジャッジ日時 2024-07-03 05:41:52
合計ジャッジ時間 25,809 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 283 ms
5,376 KB
testcase_10 AC 287 ms
5,376 KB
testcase_11 AC 319 ms
5,376 KB
testcase_12 AC 276 ms
5,376 KB
testcase_13 AC 288 ms
5,376 KB
testcase_14 AC 590 ms
5,888 KB
testcase_15 AC 933 ms
7,240 KB
testcase_16 AC 1,355 ms
8,808 KB
testcase_17 AC 961 ms
7,040 KB
testcase_18 AC 627 ms
6,016 KB
testcase_19 AC 22 ms
5,376 KB
testcase_20 AC 22 ms
5,376 KB
testcase_21 AC 22 ms
5,376 KB
testcase_22 AC 23 ms
5,376 KB
testcase_23 AC 23 ms
5,376 KB
testcase_24 AC 1,114 ms
42,516 KB
testcase_25 AC 1,223 ms
46,244 KB
testcase_26 AC 1,075 ms
41,860 KB
testcase_27 AC 1,086 ms
42,496 KB
testcase_28 AC 1,072 ms
42,640 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 1 ms
5,376 KB
testcase_35 OLE -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

// Suffix Array / Longest Common Prefix Array Construction
// Comlexity: O(N(log N)^2)
template<typename T>
struct SuffixArray
{
    T S;                    // size: N
    std::vector<int> SA;    // Suffix Array (size: N + 1, SA[0] == N) SA[i] means S[SA[i]:]
    std::vector<int> rank;  // Rank (inverse of SA) (size: N + 1, rank[N] == 0)
    std::vector<int> lcp;   // Longest Common Prefix Array (size: N) betw. S[SA[i]:] & S[SA[i + 1]:]

    SuffixArray(const T &str, bool gen_lcp = true) : S(str) {
        int N = S.size();
        SA.resize(N + 1);
        std::iota(SA.begin(), SA.end(), 0);
        rank.assign(N + 1, -1);
        for (int i = 0; i < N; i++) rank[i] = S[i];
        int _ord_mm = 1;

        auto _comp_suffarr = [&](int i, int j) {
            if (rank[i] != rank[j])
                return rank[i] < rank[j];
            int ri = i + _ord_mm < (int)rank.size() ? rank[i + _ord_mm] : -1;
            int rj = j + _ord_mm < (int)rank.size() ? rank[j + _ord_mm] : -1;
            return ri < rj;
        };
        std::vector<int> tmp(N + 1);
        for (_ord_mm = 1; _ord_mm <= N; _ord_mm *= 2) {
            std::sort(SA.begin(), SA.end(), _comp_suffarr);
            tmp[SA[0]] = 0;
            for (int i = 1; i <= N; i++) {
                tmp[SA[i]] = tmp[SA[i - 1]] + _comp_suffarr(SA[i - 1], SA[i]);
            }
            rank = tmp;
        }
        if (!gen_lcp) return;

        lcp.assign(N, 0);
        int h = 0;
        for (int i = 0; i < N; i++) {
            int j = SA[rank[i] - 1];
            if (h) h--;
            for (; j + h < N and i + h < N; h++) if (S[j + h] != S[i + h]) break;
            lcp[rank[i] - 1] = h;
        }
    }
};

int main()
{
    lint N, M, Q;
    cin >> N >> M >> Q;
    string S;
    cin >> S;
    if (M < 5) {
        string T;
        REP(_, M) T += S;
        SuffixArray<string> s(T, false);
        dbg(s.rank);
        while (Q--) {
            lint k;
            cin >> k;
            printf("%d ", s.SA[k] + 1);
        }
        puts("");
        return 0;
    }

    if (S == string(N, S[0])) {
        while (Q--) {
            lint K;
            cin >> K;
            printf("%lld ", N * M + 1 - K);
        }
        puts("");
    }

    string S4 = S + S + S + S;
    SuffixArray<string> sa4(S4, false);
    SuffixArray<string> sa5(S4 + S, false);
    vector<lint> rnkM(N * 4);
    REP(i, N * 4) rnkM[i] = sa4.rank[i] + 1LL * (sa5.rank[i + N] - sa4.rank[i]) * (M - 4);
    // unordered_map<lint, lint> mp;
    map<lint, lint> mp;
    REP(i, N * 4) mp[rnkM[i]] = N * (M - 4) + i;
    map<lint, lint> val2pos;
    REP(i, N) val2pos[rnkM[i]] = i;
    while (Q--) {
        lint k;
        cin >> k;
        if (mp.count(k)) printf("%lld ", mp[k]);
        else {
            lint i = prev(val2pos.upper_bound(k))->second;
            lint d = k - rnkM[i];
            printf("%lld ", i + 1 + N * (M - 4 - d));
        }
    }
}
0