結果

問題 No.515 典型LCP
ユーザー fumofumofunifumofumofuni
提出日時 2022-07-15 01:27:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 640 ms / 1,000 ms
コード長 3,244 bytes
コンパイル時間 2,206 ms
コンパイル使用メモリ 205,048 KB
実行使用メモリ 30,504 KB
最終ジャッジ日時 2023-09-08 17:44:50
合計ジャッジ時間 11,076 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 500 ms
30,504 KB
testcase_01 AC 581 ms
30,400 KB
testcase_02 AC 564 ms
29,100 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 510 ms
28,864 KB
testcase_06 AC 515 ms
28,892 KB
testcase_07 AC 510 ms
28,860 KB
testcase_08 AC 517 ms
29,072 KB
testcase_09 AC 504 ms
28,968 KB
testcase_10 AC 185 ms
28,996 KB
testcase_11 AC 178 ms
28,876 KB
testcase_12 AC 178 ms
28,940 KB
testcase_13 AC 544 ms
29,092 KB
testcase_14 AC 23 ms
29,100 KB
testcase_15 AC 632 ms
29,208 KB
testcase_16 AC 640 ms
29,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,1,0,-1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}

struct RollingHash {
    static const int base1 = 1007, base2 = 2009;
    static const int mod1 = 1000000007, mod2 = 1000000009;
    vector<long long> hash1, hash2, power1, power2;

    // construct
    RollingHash(const string &S) {
        int n = (int)S.size();
        hash1.assign(n+1, 0);
        hash2.assign(n+1, 0);
        power1.assign(n+1, 1);
        power2.assign(n+1, 1);
        for (int i = 0; i < n; ++i) {
            hash1[i+1] = (hash1[i] * base1 + S[i]) % mod1;
            hash2[i+1] = (hash2[i] * base2 + S[i]) % mod2;
            power1[i+1] = (power1[i] * base1) % mod1;
            power2[i+1] = (power2[i] * base2) % mod2;
        }
    }
    
    // get hash of S[left:right)
    inline pair<long long, long long> get(int l, int r) const {
        long long res1 = hash1[r] - hash1[l] * power1[r-l] % mod1;
        if (res1 < 0) res1 += mod1;
        long long res2 = hash2[r] - hash2[l] * power2[r-l] % mod2;
        if (res2 < 0) res2 += mod2;
        return {res1, res2};
    }
    using pl=pair<long long,long long>;
    inline pl connect(pl h1,pl h2,int h2len){
        pl ret;
        ret.first=(h1.first*power1[h2len]+h2.first)%mod1;
        ret.second=(h1.second*power2[h2len]+h2.second)%mod2;
        return ret;
    }

    // get lcp of S[a:] and T[b:]
    inline int getLCP(int a, int b) const {
        int len = min((int)hash1.size()-a, (int)hash1.size()-b);
        int low = 0, high = len;
        while (high - low > 1) {
            int mid = (low + high) >> 1;
            if (get(a, a+mid) != get(b, b+mid)) high = mid;
            else low = mid;
        }
        return low;
    }
};


int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll n;cin >> n;
  vl len(n);
  vl start(n);
  string s;
  rep(i,n){
    string t;cin >> t;
    len[i]=t.size();
    start[i]=s.size();
    s+=t;
  }
  RollingHash rh(s);
  ll m;cin >> m;
  ll x,d;cin >> x >> d;
  ll ans=0;
  rep(i,m){
    ll f=(x/(n-1))+1;
    ll r=(x%(n-1))+1;
    if(f>r)swap(f,r);
    else r+=1;
    x=(x+d)%(n*(n-1));
    f--;r--;
    ll ok=0,ng=min(len[f],len[r])+1;
    while(ng-ok>1){
      ll mid=(ok+ng)>>1;
      if(rh.get(start[f],start[f]+mid)==rh.get(start[r],start[r]+mid))ok=mid;
      else ng=mid;
    }
    ans+=ok;
  }
  cout << ans << endl;

} 
0