結果

問題 No.1018 suffixsuffixsuffix
ユーザー 👑 hitonanodehitonanode
提出日時 2020-04-03 23:19:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,059 ms / 2,000 ms
コード長 6,626 bytes
コンパイル時間 2,085 ms
コンパイル使用メモリ 186,796 KB
実行使用メモリ 37,548 KB
最終ジャッジ日時 2023-09-16 04:50:28
合計ジャッジ時間 13,427 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 75 ms
4,420 KB
testcase_10 AC 77 ms
4,688 KB
testcase_11 AC 80 ms
4,608 KB
testcase_12 AC 70 ms
4,416 KB
testcase_13 AC 72 ms
4,376 KB
testcase_14 AC 167 ms
5,744 KB
testcase_15 AC 294 ms
7,088 KB
testcase_16 AC 448 ms
8,752 KB
testcase_17 AC 268 ms
7,156 KB
testcase_18 AC 164 ms
5,916 KB
testcase_19 AC 22 ms
4,376 KB
testcase_20 AC 22 ms
4,376 KB
testcase_21 AC 22 ms
4,376 KB
testcase_22 AC 23 ms
4,376 KB
testcase_23 AC 25 ms
4,380 KB
testcase_24 AC 942 ms
34,436 KB
testcase_25 AC 1,031 ms
37,440 KB
testcase_26 AC 940 ms
33,796 KB
testcase_27 AC 920 ms
34,668 KB
testcase_28 AC 931 ms
34,516 KB
testcase_29 AC 29 ms
4,380 KB
testcase_30 AC 29 ms
4,376 KB
testcase_31 AC 29 ms
4,380 KB
testcase_32 AC 29 ms
4,376 KB
testcase_33 AC 28 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 23 ms
4,376 KB
testcase_36 AC 29 ms
4,380 KB
testcase_37 AC 1,059 ms
37,548 KB
権限があれば一括ダウンロードができます

ソースコード

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;
        }
    }
};

// Z algorithm (length of longest common prefix for s[0:N] & s[i:N] for each i)
// Complexity: O(N)
// <http://snuke.hatenablog.com/entry/2014/12/03/214243>
vector<int> z_algorithm(const string &s) {
    vector<int> ans(s.size());
    ans[0] = s.size();
    int i = 1, j = 0;
    while (i < (int)s.size()) {
        while (i + j < (int)s.size() && s[j] == s[i + j]) ++j;
        ans[i] = j;
        if (!j) {
            ++i;
            continue;
        }
        int k = 1;
        while (i + k < (int)s.size() && k + ans[k] < j) ans[i + k] = ans[k], ++k;
        i += k;
        j -= k;
    }
    return ans;
}

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);
        while (Q--) {
            int k;
            cin >> k;
            printf("%d ", s.SA[k] + 1);
        }
        puts("");
        return 0;
    }

    auto z = z_algorithm(S);
    int rec = S.length();
    REP(i, N) if (i and z[i] + i == N and N % i == 0) chmin(rec, i);

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

    string S3 = S + S + S;
    SuffixArray<string> sa3(S3, false);
    SuffixArray<string> sa4(S3 + S, false);
    vector<lint> rnkM(N * 3);
    REP(i, N * 3) rnkM[i] = sa3.rank[i] + 1LL * (sa4.rank[i + N] - sa3.rank[i]) * (M - 3);
    map<lint, lint> mp;
    REP(i, N * 3) mp[rnkM[i]] = N * (M - 3) + 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 - 3 - d));
        }
    }
}
0