結果

問題 No.1018 suffixsuffixsuffix
ユーザー kyort0nkyort0n
提出日時 2020-04-03 22:55:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,559 bytes
コンパイル時間 2,598 ms
コンパイル使用メモリ 189,320 KB
実行使用メモリ 8,668 KB
最終ジャッジ日時 2024-07-03 05:29:04
合計ジャッジ時間 6,640 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 AC 25 ms
6,940 KB
testcase_20 AC 25 ms
6,940 KB
testcase_21 AC 26 ms
6,940 KB
testcase_22 AC 26 ms
6,940 KB
testcase_23 AC 26 ms
6,940 KB
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 AC 886 ms
8,552 KB
testcase_30 AC 827 ms
8,400 KB
testcase_31 AC 813 ms
8,392 KB
testcase_32 AC 830 ms
8,268 KB
testcase_33 AC 887 ms
8,668 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 21 ms
6,944 KB
testcase_36 AC 26 ms
6,940 KB
testcase_37 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
//const ll mod = 1000000007;
struct SuffixArray {
  vector< int > SA;
  const string s;

  SuffixArray(const string &str) : s(str) {
    SA.resize(s.size());
    iota(begin(SA), end(SA), 0);
    sort(begin(SA), end(SA), [&](int a, int b) {
      return s[a] == s[b] ? a > b : s[a] < s[b];
    });
    vector< int > classes(s.size()), c(s.begin(), s.end()), cnt(s.size());
    for(int len = 1; len < s.size(); len <<= 1) {
      for(int i = 0; i < s.size(); i++) {
        if(i > 0 && c[SA[i - 1]] == c[SA[i]] && SA[i - 1] + len < s.size() && c[SA[i - 1] + len / 2] == c[SA[i] + len / 2]) {
          classes[SA[i]] = classes[SA[i - 1]];
        } else {
          classes[SA[i]] = i;
        }
      }
      iota(begin(cnt), end(cnt), 0);
      copy(begin(SA), end(SA), begin(c));
      for(int i = 0; i < s.size(); i++) {
        int s1 = c[i] - len;
        if(s1 >= 0) SA[cnt[classes[s1]]++] = s1;
      }
      classes.swap(c);
    }
  }

  int operator[](int k) const {
    return SA[k];
  }

  size_t size() const {
    return s.size();
  }

  bool lt_substr(const string &t, int si = 0, int ti = 0) {
    int sn = (int) s.size(), tn = (int) t.size();
    while(si < sn && ti < tn) {
      if(s[si] < t[ti]) return true;
      if(s[si] > t[ti]) return false;
      ++si, ++ti;
    }
    return si >= sn && ti < tn;
  }

  int lower_bound(const string &t) {
    int low = -1, high = (int) SA.size();
    while(high - low > 1) {
      int mid = (low + high) / 2;
      if(lt_substr(t, SA[mid])) low = mid;
      else high = mid;
    }
    return high;
  }

  pair< int, int > lower_upper_bound(string &t) {
    int idx = lower_bound(t);
    int low = idx - 1, high = (int) SA.size();
    t.back()++;
    while(high - low > 1) {
      int mid = (low + high) / 2;
      if(lt_substr(t, SA[mid])) low = mid;
      else high = mid;
    }
    t.back()--;
    return {idx, high};
  }

  void output() {
    for(int i = 0; i < size(); i++) {
      cout << i << ": " << s.substr(SA[i]) << endl;
    }
  }
};

struct LongestCommonPrefixArray {
  const SuffixArray &SA;
  vector< int > LCP, rank;

  LongestCommonPrefixArray(const SuffixArray &SA) : SA(SA), LCP(SA.size()) {
    rank.resize(SA.size());
    for(int i = 0; i < SA.size(); i++) {
      rank[SA[i]] = i;
    }
    for(int i = 0, h = 0; i < SA.size(); i++) {
      if(rank[i] + 1 < SA.size()) {
        for(int j = SA[rank[i] + 1]; max(i, j) + h < SA.size() && SA.s[i + h] == SA.s[j + h]; ++h);
        LCP[rank[i] + 1] = h;
        if(h > 0) --h;
      }
    }
  }

  int operator[](int k) const {
    return LCP[k];
  }

  size_t size() const {
    return LCP.size();
  }

  void output() {
    for(int i = 0; i < size(); i++) {
      cout << i << ": " << LCP[i] << " " << SA.s.substr(SA[i]) << endl;
    }
  }
};

bool AllSame(string S) {
    sort(S.begin(), S.end());
    return S[0] == S.back();
}

map<ll, l_l> mp;
ll ans[100050];

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, M, Q;
    cin >> N >> M >> Q;
    string S;
    cin >> S;
    if(AllSame(S)) {
        for(int q = 0; q < Q; q++) {
            if(q != 0) cout << " ";
            ll K;
            cin >> K;
            cout << N * M - K + 1;
        }
        cout << endl;
        return 0;
    }
    S = S + S + S;
    SuffixArray SA(S);
    for(int i = 0; i < S.size(); i++) {
        cerr << SA[i] << " ";
    }
    cerr << endl;
    ll nowone = 0;
    ll nowrep = 0;
    vector<l_l> Query(Q);
    for(int i = 0; i < Q; i++) {
        Query[i].second = i;
        cin >> Query[i].first;
        Query[i].first--;
    }
    sort(Query.begin(), Query.end());
    ll nownum = 0;
    ll idx = 0;
    for(int i = 0; i < S.size(); i++) {
        if(SA[i] < 2 * N) {
            //mp[SA[i]-N] = {nowrep, nowone};
            ll delta = SA[i] - SA[i+1];
            ll tmp = SA[i] - N + N * (M - 2);
            ll tmpnum = tmp / delta + 1;
            cerr << "main: " << i << " " << tmp << " " << idx << " " << delta << " " << tmpnum << endl;
            assert(SA[i] >= N);
            while(idx < Q and nownum + tmpnum - 1 >= Query[idx].first) {
                //cerr << idx << " " << i << endl;
                ll tmp = SA[i] - N;
                tmp += N * (M - 2);
                //cerr << idx << " " << tmp << endl;
                tmp -= (Query[idx].first - nownum) * delta;
                //cerr << idx << " " << tmp << endl;
                ans[Query[idx].second] = tmp;
                idx++;
            }
            nowrep++;
            nownum += tmpnum;
            i += SA[i] / delta + 1 - 1;
        } else {
            //mp[SA[i]] = {nowrep, nowone};
            //cerr << "sub:" << i << " " << nownum << endl;
            if(idx < Q and Query[idx].first == nownum) {
                ll tmp = SA[i] + N * (M - 3);
                ans[Query[idx].second] = tmp;
                idx++;
            }
            nowone++;
            nownum++;
        }
    }
    for(int i = 0; i < Q; i++) {
        if(i != 0) cout << " ";
        cout << ans[i] + 1;
    }
    cout << endl;
    return 0;
}
0