結果
問題 | No.2454 Former < Latter |
ユーザー | shobonvip |
提出日時 | 2023-09-01 22:03:44 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 78 ms / 2,000 ms |
コード長 | 2,819 bytes |
コンパイル時間 | 4,855 ms |
コンパイル使用メモリ | 255,168 KB |
実行使用メモリ | 19,532 KB |
最終ジャッジ日時 | 2025-01-03 08:41:03 |
合計ジャッジ時間 | 6,755 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 23 |
ソースコード
#include<bits/stdc++.h> using namespace std; //* ATCODER #include<atcoder/all> using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include<boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template <typename T> bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template <typename T> bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template <typename T> T max(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template <typename T> T min(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template <typename T> T sum(vector<T> &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } // isprime bool isprime(ll n){ if (n == 1) return false; for (ll i = 2; i * i <= n; i++){ if (n % i == 0) return false; } return true; } // importrandom // don't forget randomjumon // and also isprime ll RandomMod(ll l, ll r){ ll ret = l + rand() % (r - l); while (!isprime(ret)) ret = l + rand() % (r - l); return ret; } ll randrange(ll l, ll r){ return l + rand() % (r - l); } // ----- int main(){ srand((unsigned)time(NULL)); // Rolling Hash // don't forget randomjumon and importrandom typedef dynamic_modint<0> mint1; typedef dynamic_modint<1> mint2; int mod1 = RandomMod(7e8, 1e9); int mod2; do{ mod2 = RandomMod(7e8, 1e9); }while(mod1 == mod2); mint1::set_mod(mod1); mint2::set_mod(mod2); mint1 b1 = randrange(100, 200); mint2 b2 = randrange(100, 200); int MAX_m = 800000; vector<mint1> b1l(MAX_m+1); b1l[0] = 1; for (int i=0; i<MAX_m; i++){ b1l[i+1] = b1l[i] * b1; } vector<mint2> b2l(MAX_m+1); b2l[0] = 1; for (int i=0; i<MAX_m; i++){ b2l[i+1] = b2l[i] * b2; } // ----- ios_base::sync_with_stdio(false); cin.tie(NULL); int CASES; cin >> CASES; while(CASES--){ int n; cin >> n; string s; cin >> s; int ans = 0; vector<mint1> t1(n+1); vector<mint2> t2(n+1); rep(i,0,n){ t1[i+1] = t1[i] * b1 + s[i] - 'a' + 1; t2[i+1] = t2[i] * b2 + s[i] - 'a' + 1; } vector<int> sa = suffix_array(s); vector<int> q(n); rep(i,0,n){ q[sa[i]] = i; } rep(i,1,n){ int v = min(i, n-i); mint1 x1 = t1[v] - t1[0] * b1l[v]; mint1 y1 = t1[i+v] - t1[i] * b1l[v]; mint2 x2 = t2[v] - t2[0] * b2l[v]; mint2 y2 = t2[i+v] - t2[i] * b2l[v]; if (x2 == y2 && y1 == x1){ if (i < n-i){ ans++; } }else{ if (q[0] < q[i]){ ans++; } } } cout << ans << '\n'; } }