結果

問題 No.515 典型LCP
ユーザー しらっ亭しらっ亭
提出日時 2017-05-06 00:56:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 6,527 bytes
コンパイル時間 1,919 ms
コンパイル使用メモリ 179,408 KB
実行使用メモリ 45,940 KB
最終ジャッジ日時 2023-10-12 10:57:17
合計ジャッジ時間 8,155 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x,arr) for(auto&& x:arr)
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define bit(n) (1LL<<(n))
#define sz(x) ((int)(x).size())
#define ten(n) ((int)1e##n)
#define fst first
#define snd second
using ll=long long;
using pii=pair<int,int>;using pll=pair<ll,ll>;using pil=pair<int,ll>;using pli=pair<ll,int>;
using vs=vector<string>;using vvs=vector<vs>;using vvvs=vector<vvs>;
using vb=vector<bool>;using vvb=vector<vb>;using vvvb=vector<vvb>;
using vi=vector<int>;using vvi=vector<vi>;using vvvi=vector<vvi>;
using vl=vector<ll>;using vvl=vector<vl>;using vvvl=vector<vvl>;
using vd=vector<double>;using vvd=vector<vd>;using vvvd=vector<vvd>;
using vpii=vector<pii>;using vvpii=vector<vpii>;using vvvpii=vector<vvpii>;
template<class T> bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
ll ri(){ll l;cin>>l;return l;} string rs(){string s;cin>>s;return s;}
template<class T>T read(){T t;cin>>t;return t;}
template<class T,class U>ostream&operator<<(ostream&o,const pair<T,U>&p){return o<<'('<<p.fst<<", "<<p.snd<<')';}
ostream&operator<<(ostream&o,const vb&t){forr(e,t)o<<"#."[e];return o;}
template<class T>ostream&operator<<(ostream&o,const vector<T>&t){o<<"{";forr(e,t)o<<e<<",";o<<"}"<<endl;return o;}
#ifdef LOCAL
vs s_p_l_i_t(const string&s,char c){vs v;stringstream ss(s);string x;while(getline(ss,x,c))v.emplace_back(x);return move(v);}
void e_r_r(vs::iterator it){cerr<<endl;}
template<class T,class... Args>void e_r_r(vs::iterator it,T a,Args... args){cerr<<it->substr((*it)[0]==' ',it->length())<<" = "<<a<<", ";e_r_r(++it,args...);}
#define out(args...){vs a_r_g_s=s_p_l_i_t(#args,',');e_r_r(a_r_g_s.begin(),args);}
#else
#define out(args...)
#endif

/**
 * ハッシュ衝突によるチャレンジに注意(ランダムな素数のmodを選び、mod以下の整数となるbaseを選ぶこと)
 */
class RollingHash {
  public:
    int N, M;
    vector<long long> mods, base;
    vector<vector<long long>> P, H;

    void add_mod(int mod, int bas) {
      mods.push_back(mod);
      base.push_back(bas);
    }

    /**
     * O(NM)
     */
    void initialize(const string& S) {
      assert(mods.size());
      N = S.length();
      M = mods.size();
      P.resize(M, vector<long long>(N + 1, 1LL));
      H.resize(M, vector<long long>(N + 1, 0LL));

      for (int m = 0; m < M; m++) {
        for (int i = 0; i < N; i++) {
          int c = (int) (S[i] - ' ');
          H[m][i+1] = (H[m][i] * base[m] % mods[m] + c) % mods[m];
          P[m][i+1] = P[m][i] * base[m] % mods[m];
        }
      }
    }

    /**
     * S の [0, N) のハッシュ値を返す
     * O(1)
     */
    long long hash(int m) const {
      return H[m][N];
    }

    /**
     * S の [0, r) のハッシュ値を返す
     * O(1)
     */
    long long hash(int r, int m) const {
      return H[m][r];
    }

    /**
     * S の [l, r) のハッシュ値を返す
     * O(1)
     */
    long long hash(int l, int r, int m) const {
      long long ret = H[m][r] - (H[m][l] * P[m][r - l]) % mods[m];
      if (ret < 0) ret += mods[m];
      else if (ret > mods[m]) ret %= mods[m];
      return ret;
    }

    /**
     * S の [0, N) のハッシュ値一覧を返す
     * O(M)
     */
    vector<long long> hashes() const {
      vector<long long> ret;
      for (int m = 0; m < M; m++) ret.push_back(hash(m));
      return ret;
    }

    /**
     * S の [l, r) のハッシュ値一覧を返す
     * O(M)
     */
    vector<long long> hashes(int r) const {
      vector<long long> ret;
      for (int m = 0; m < M; m++) ret.push_back(hash(r, m));
      return ret;
    }

    /**
     * S の [l, r) のハッシュ値一覧を返す
     * O(M)
     */
    vector<long long> hashes(int l, int r) const {
      vector<long long> ret;
      for (int m = 0; m < M; m++) ret.push_back(hash(l, r, m));
      return ret;
    }

    /**
     * S の [l1, r1) と [l2, r2) のハッシュ値が等しいかを返す
     * O(1)
     */
    bool equals(int l1, int r1, int l2, int r2) const {
      if (l1 < 0 || l2 < 0) return false;
      if (r1 > N || r2 > N) return false;
      if (r1 - l1 != r2 - l2) return false;
      for (int m = 0; m < M; m++) {
        if (hash(l1, r1, m) != hash(l2, r2, m)) return false;
      }
      return true;
    }

    /**
     * S の p1 からの len 文字と p2 からの len 文字が等しいかを返す
     * O(1)
     */
    bool equals(int p1, int p2, int len) const {
      return equals(p1, p1 + len, p2, p2 + len);
    }

    /**
     * S の pos 以降と other が等しいかを返す
     * O(1)
     */
    bool equals(int pos, const RollingHash &other) const {
      int r = pos + other.N;
      if (r >= N) return false;

      for (int m = 0; m < M; m++) {
        if (hash(pos, r, m) != hash(0, other.N, m)) return false;
      }
      return true;
    }

    /**
     * S の p1 からと p2 からで共通する文字列の長さを返す。
     * O(log(N))
     */
    int lcp(int p1, int p2) const {
      int ok = 0;
      int ng = min(N - p1, N - p2) + 1;
      while (ng - ok > 1) {
        int mid = (ok + ng) / 2;
        (equals(p1, p1 + mid, p2, p2 + mid) ? ok : ng) = mid;
      }
      return ok;
    }
};

void Main() {
  ll n = ri();

  int mod = 1e9+9;
  int base = 114511;

  vector<RollingHash> rh(n);
  rep(i, n) {
    rh[i].add_mod(mod, base);
    rh[i].initialize(rs());
  }

  ll m = ri();
  ll x = ri();
  ll d = ri();

  ll ans = 0;

  rep(k, 1, m+1) {
    ll i = (x / (n - 1)) + 1;
    ll j = (x % (n - 1)) + 1;
    if (i > j) swap(i, j);
    else j++;
    x = (x + d) % (n * (n - 1));

    RollingHash &r1 = rh[i-1];
    RollingHash &r2 = rh[j-1];

    {
      int ok = 0;
      int ng = min(r1.N, r2.N) + 1;
      while (ng - ok > 1) {
        int mid = (ok + ng) / 2;
        (r1.hashes(mid) == r2.hashes(mid) ? ok : ng) = mid;
      }
      ans += ok;
    }
  }
  cout << ans << endl;
  
}
int main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }
0