結果

問題 No.515 典型LCP
ユーザー miyo2580miyo2580
提出日時 2023-11-01 21:52:01
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 288 ms / 1,000 ms
コード長 2,211 bytes
コンパイル時間 3,566 ms
コンパイル使用メモリ 266,392 KB
実行使用メモリ 22,208 KB
最終ジャッジ日時 2023-11-01 21:52:08
合計ジャッジ時間 6,573 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 287 ms
22,208 KB
testcase_01 AC 288 ms
22,208 KB
testcase_02 AC 131 ms
4,644 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 83 ms
4,348 KB
testcase_06 AC 83 ms
4,584 KB
testcase_07 AC 83 ms
4,380 KB
testcase_08 AC 138 ms
4,348 KB
testcase_09 AC 85 ms
4,644 KB
testcase_10 AC 86 ms
4,504 KB
testcase_11 AC 86 ms
4,504 KB
testcase_12 AC 86 ms
4,504 KB
testcase_13 AC 87 ms
4,348 KB
testcase_14 AC 5 ms
4,348 KB
testcase_15 AC 82 ms
4,408 KB
testcase_16 AC 82 ms
4,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (ll i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vec;
using Graph = vector<vec>;
const long long INF = 1LL<<60;
const long long MOD = 1000000007;

#include <cassert>
#include <limits>
#include <vector>
using namespace std;

template <typename T>
struct SparseTable {
  inline static constexpr T INF = numeric_limits<T>::max() / 2;
  int N;
  vector<vector<T> > table;
  T f(T a, T b) { return min(a, b); }
  SparseTable() {}
  SparseTable(const vector<T> &v) : N(v.size()) {
    int b = 1;
    while ((1 << b) <= N) ++b;
    table.push_back(v);
    for (int i = 1; i < b; i++) {
      table.push_back(vector<T>(N, INF));
      for (int j = 0; j + (1 << i) <= N; j++) {
        table[i][j] = f(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
      }
    }
  }
  // [l, r)
  T query(int l, int r) {
    assert(0 <= l and l <= r and r <= N);
    if (l == r) return INF;
    int b = 31 - __builtin_clz(r - l);
    return f(table[b][l], table[b][r - (1 << b)]);
  }
};


int main()
{  
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;cin>>n;
    vector<pair<string,int>> s(n);
    rep(i,n){
        cin>>s[i].first;
        s[i].second=i;
    }
    sort(all(s));
    vec lcp(n-1);
    rep(i,n-1){
        string s1=s[i].first;
        string s2=s[i+1].first;
        ll x=min(s1.size(),s2.size());
        ll now=0;
        rep(j,x){
            if(s1[j]!=s2[j])break;
            now++;
        }
        lcp[i]=now;
    }
    vec memo(n);
    rep(i,n){
        memo[s[i].second]=i;
    }
    SparseTable st(lcp);
    ll m,x,d;cin>>m>>x>>d;
    ll ans=0;
    rep(i,m){
        ll I=(x/(n-1))+1;
        ll J=(x%(n-1))+1;
        if(I>J)swap(I,J);
        else J++;
        I--;J--;
        if(memo[I]>memo[J])swap(I,J);
        ans+=st.query(memo[I],memo[J]);
        x=(x+d)%(n*(n-1));
    }
    cout<<ans<<endl;
    return 0;
}
0