結果
問題 | No.515 典型LCP |
ユーザー | __math |
提出日時 | 2017-05-05 23:26:49 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 901 ms / 1,000 ms |
コード長 | 8,931 bytes |
コンパイル時間 | 2,276 ms |
コンパイル使用メモリ | 183,360 KB |
実行使用メモリ | 104,192 KB |
最終ジャッジ日時 | 2024-09-14 10:00:03 |
合計ジャッジ時間 | 9,361 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 901 ms
104,192 KB |
testcase_01 | AC | 496 ms
97,800 KB |
testcase_02 | AC | 249 ms
83,128 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 379 ms
91,332 KB |
testcase_06 | AC | 386 ms
91,340 KB |
testcase_07 | AC | 378 ms
88,268 KB |
testcase_08 | AC | 403 ms
91,204 KB |
testcase_09 | AC | 209 ms
83,216 KB |
testcase_10 | AC | 205 ms
83,116 KB |
testcase_11 | AC | 204 ms
83,300 KB |
testcase_12 | AC | 205 ms
83,272 KB |
testcase_13 | AC | 203 ms
86,188 KB |
testcase_14 | AC | 119 ms
83,156 KB |
testcase_15 | AC | 363 ms
88,252 KB |
testcase_16 | AC | 367 ms
88,136 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define FOR(i,n) for(int i = 0; i < (n); i++) #define sz(c) ((int)c.size()) #define ten(n) ((int)1e##n) using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; template<typename ...> static inline int getchar_unlocked(void) { return getchar(); } template<typename ...> static inline void putchar_unlocked(int c) { putchar(c); } #define mygc(c) (c)=getchar_unlocked() #define mypc(c) putchar_unlocked(c) void reader(int& x) { int k, m = 0; x = 0; for (;;) { mygc(k); if (k == '-') { m = 1; break; }if ('0' <= k&&k <= '9') { x = k - '0'; break; } }for (;;) { mygc(k); if (k<'0' || k>'9')break; x = x * 10 + k - '0'; }if (m) x = -x; } void reader(ll& x) { int k, m = 0; x = 0; for (;;) { mygc(k); if (k == '-') { m = 1; break; }if ('0' <= k&&k <= '9') { x = k - '0'; break; } }for (;;) { mygc(k); if (k<'0' || k>'9')break; x = x * 10 + k - '0'; }if (m) x = -x; } int reader(char c[]) { int i, s = 0; for (;;) { mygc(i); if (i != ' '&&i != '\n'&&i != '\r'&&i != '\t'&&i != EOF) break; }c[s++] = i; for (;;) { mygc(i); if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF) break; c[s++] = i; }c[s] = '\0'; return s; } int reader(string& c) { int i; for (;;) { mygc(i); if (i != ' '&&i != '\n'&&i != '\r'&&i != '\t'&&i != EOF) break; }c.push_back(i); for (;;) { mygc(i); if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF) break; c.push_back(i); }; return sz(c); } template <class T, class S> void reader(T& x, S& y) { reader(x); reader(y); } template <class T, class S, class U> void reader(T& x, S& y, U& z) { reader(x); reader(y); reader(z); } template <class T, class S, class U, class V> void reader(T& x, S& y, U& z, V & w) { reader(x); reader(y); reader(z); reader(w); } void writer(int x, char c) { int s = 0, m = 0; char f[10]; if (x<0)m = 1, x = -x; while (x)f[s++] = x % 10, x /= 10; if (!s)f[s++] = 0; if (m)mypc('-'); while (s--)mypc(f[s] + '0'); mypc(c); } void writer(ll x, char c) { int s = 0, m = 0; char f[20]; if (x<0)m = 1, x = -x; while (x)f[s++] = x % 10, x /= 10; if (!s)f[s++] = 0; if (m)mypc('-'); while (s--)mypc(f[s] + '0'); mypc(c); } void writer(const char c[]) { int i; for (i = 0; c[i] != '\0'; i++)mypc(c[i]); } void writer(const string& x, char c) { int i; for (i = 0; x[i] != '\0'; i++)mypc(x[i]); mypc(c); } void writer(const char x[], char c) { int i; for (i = 0; x[i] != '\0'; i++)mypc(x[i]); mypc(c); } template<class T> void writerLn(T x) { writer(x, '\n'); } template<class T, class S> void writerLn(T x, S y) { writer(x, ' '); writer(y, '\n'); } template<class T, class S, class U> void writerLn(T x, S y, U z) { writer(x, ' '); writer(y, ' '); writer(z, '\n'); } template<class T> void writerArr(T x[], int n) { if (!n) { mypc('\n'); return; }FOR(i, n - 1)writer(x[i], ' '); writer(x[n - 1], '\n'); } template<class T> void writerArr(vector<T>& x) { writerArr(x.data(), (int)x.size()); } template<class T> void chmin(T& a, const T& b) { if (a > b) a = b; } template<class T> void chmax(T& a, const T& b) { if (a < b) a = b; } template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; } template<class T> T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; } template<class T> T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; } class sparse_table { vector<vector<int>> dat; vector<int> logTable; int n; int zero_element() const { return numeric_limits<int>::max(); } int prop(int a, int b) const { return min(a, b); } public: void init(vector<int>& a) { n = sz(a); logTable.resize(n + 1); logTable[0] = logTable[1] = 0; for (int i = 2; i <= n; i++) logTable[i] = logTable[i >> 1] + 1; int logN = logTable[n]; dat.resize(logN + 1, vector<int>(n, zero_element())); FOR(i, n) dat[0][i] = a[i]; for (int i = 1; i <= logN; i++) { int l = 1 << i; for (int j = 0; j + l <= n; j++) { dat[i][j] = prop(dat[i - 1][j], dat[i - 1][j + l / 2]); } } } int query(int l, int r) { int k = logTable[r - l]; return prop(dat[k][l], dat[k][r - (1 << k)]); } }; //1-originにして末尾に0を加えてから呼ぶのを忘れないこと vector<int> buildSA(const vector<int>& vec) { if (vec.empty()) return{}; vector<bool> dat(sz(vec)); // dat[vec.size() - 1] = 0; for (int i = vec.size() - 2; i >= 0; i--) { if (vec[i] > vec[i + 1]) dat[i] = true; else if (vec[i] < vec[i + 1]) dat[i] = false; else dat[i] = dat[i + 1]; } int maxi = 0; FOR(i, sz(vec)) maxi = max(maxi, vec[i]); vector<int> vnum(maxi + 1); FOR(i, sz(vec)) vnum[vec[i]]++; vector<vector<int>> sa(maxi + 1); FOR(i, maxi + 1) sa[i].resize(vnum[i], -1); vector<int> pt1(maxi + 1), pt2(maxi + 1); FOR(i, maxi + 1) pt2[i] = vnum[i] - 1; vector<bool> islms(sz(vec)); for (int i = vec.size() - 1; i >= 1; i--) { if ((dat[i - 1] && !dat[i]) || i == vec.size() - 1) { sa[vec[i]][pt2[vec[i]]--] = i; islms[i] = true; } } FOR(i, maxi + 1) FOR(j, vnum[i]) { if (sa[i][j] > 0) { if (dat[sa[i][j] - 1]) { sa[vec[sa[i][j] - 1]][pt1[vec[sa[i][j] - 1]]++] = sa[i][j] - 1; } } } for (int i = 1; i <= maxi; i++) { for (int j = pt2[i] + 1; j < vnum[i]; j++) sa[i][j] = -1; pt2[i] = vnum[i] - 1; } for (int i = maxi; i >= 0; i--) { for (int j = vnum[i] - 1; j >= 0; j--) { if (sa[i][j] > 0) { if (!dat[sa[i][j] - 1]) { sa[vec[sa[i][j] - 1]][pt2[vec[sa[i][j] - 1]]--] = sa[i][j] - 1; } } } } vector<int> d(sz(vec)); int cnt = 0; vector<int> bef; bef.push_back(0); bool fl = false; FOR(i, maxi + 1) FOR(j, vnum[i]) { if (islms[sa[i][j]]) { vector<int> zk; zk.push_back(vec[sa[i][j]]); for (int k = sa[i][j] + 1; k < sz(vec); k++) { zk.push_back(vec[k]); if (islms[k]) break; } if (zk != bef) cnt++; else if (vec[sa[i][j]] != 0) fl = true; d[sa[i][j]] = cnt; bef = zk; } } vector<int> vt; FOR(i, sz(vec)) if (islms[i]) vt.push_back(d[i]); vector<int> gv, nv; if (fl) { gv = buildSA(vt); vector<int> v; FOR(i, sz(vec)) if (islms[i]) v.push_back(i); FOR(i, sz(gv)) nv.push_back(v[gv[i]]); } else { gv = vt; nv.resize(gv.size()); int pt = 0; FOR(i, sz(gv)) { while (true) { if (islms[pt]) { nv[gv[i]] = pt; pt++; break; } pt++; } } } for (int i = 0; i <= maxi; i++) { fill(sa[i].begin(), sa[i].end(), -1); pt1[i] = 0; pt2[i] = vnum[i] - 1; } for (int i = sz(nv) - 1; i >= 0; i--) { sa[vec[nv[i]]][pt2[vec[nv[i]]]--] = nv[i]; } for (int i = 0; i <= maxi; i++) { for (int j = 0; j < vnum[i]; j++) { if (sa[i][j] > 0) { if (dat[sa[i][j] - 1]) { sa[vec[sa[i][j] - 1]][pt1[vec[sa[i][j] - 1]]++] = sa[i][j] - 1; } } } } for (int i = 1; i <= maxi; i++) { for (int j = pt2[i] + 1; j < vnum[i]; j++)sa[i][j] = -1; pt2[i] = vnum[i] - 1; } for (int i = maxi; i >= 0; i--) { for (int j = vnum[i] - 1; j >= 0; j--) { if (sa[i][j] > 0) { if (!dat[sa[i][j] - 1]) { sa[vec[sa[i][j] - 1]][pt2[vec[sa[i][j] - 1]]--] = sa[i][j] - 1; } } } } vector<int> ret; FOR(i, maxi + 1) FOR(j, vnum[i]) ret.push_back(sa[i][j]); return ret; } //lcp: SAのi番目とi+1番目のlcp vector<int> calclcp(const vector<int>& str, vector<int>& sa) { vector<int> rsa(sz(sa)); FOR(i, sz(sa)) rsa[sa[i]] = i; vector<int> lcp(sz(sa)); int now = 1; for (int i = 0; i < sz(str) - 1; i++) { if (now != 0) now--; while(true) { if (str[i + now] == str[sa[rsa[i] - 1] + now]) now++; else { if(rsa[i]) lcp[rsa[i] - 1] = now; break; } } } return lcp; } class lcp_sparse_table { string s; vector<int> sa, isa, lcp; sparse_table sp; public: lcp_sparse_table(string& s) : s(s) { vector<int> sint; for (auto c : s) sint.push_back(c); sint.push_back(0); sa = buildSA(sint); lcp = calclcp(sint, sa); sp.init(lcp); isa.resize(sz(sa)); FOR(i, sz(sa)) isa[sa[i]] = i; } int query(int l, int r) { int il = isa[l], ir = isa[r]; if (il > ir) swap(il, ir); return sp.query(il, ir); } }; int main() { int n; reader(n); string s; vector<int> idx; vector<int> orilen; FOR(i, n) { idx.push_back(sz(s)); string a; reader(a); for (auto c : a) s.push_back(c); s.push_back(';'); orilen.push_back(sz(a)); } lcp_sparse_table lst(s); int m; ll x, d; reader(m, x, d); const ll nmod = ll(n) * (n - 1); ll ans = 0; FOR(_, m) { int i = (x / (n - 1)) + 1; int j = (x % (n - 1)) + 1; if (i > j) swap(i, j); else j++; x = (x + d) % nmod; i--; j--; int len = lst.query(idx[i], idx[j]); len = min(len, min(orilen[i], orilen[j])); ans += len; } writerLn(ans); return 0; }