結果
問題 | No.2454 Former < Latter |
ユーザー | k1suxu |
提出日時 | 2023-09-01 23:30:50 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 687 ms / 2,000 ms |
コード長 | 5,930 bytes |
コンパイル時間 | 3,807 ms |
コンパイル使用メモリ | 255,184 KB |
実行使用メモリ | 8,244 KB |
最終ジャッジ日時 | 2024-06-11 05:39:58 |
合計ジャッジ時間 | 6,064 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 71 ms
7,980 KB |
testcase_03 | AC | 71 ms
8,120 KB |
testcase_04 | AC | 75 ms
8,088 KB |
testcase_05 | AC | 73 ms
8,244 KB |
testcase_06 | AC | 67 ms
7,224 KB |
testcase_07 | AC | 68 ms
7,368 KB |
testcase_08 | AC | 34 ms
6,940 KB |
testcase_09 | AC | 40 ms
6,944 KB |
testcase_10 | AC | 45 ms
6,944 KB |
testcase_11 | AC | 72 ms
8,120 KB |
testcase_12 | AC | 71 ms
8,120 KB |
testcase_13 | AC | 72 ms
8,120 KB |
testcase_14 | AC | 72 ms
7,996 KB |
testcase_15 | AC | 60 ms
6,940 KB |
testcase_16 | AC | 58 ms
6,940 KB |
testcase_17 | AC | 687 ms
6,944 KB |
testcase_18 | AC | 37 ms
6,944 KB |
testcase_19 | AC | 66 ms
7,480 KB |
testcase_20 | AC | 69 ms
7,476 KB |
testcase_21 | AC | 36 ms
6,940 KB |
testcase_22 | AC | 42 ms
6,940 KB |
testcase_23 | AC | 40 ms
6,940 KB |
ソースコード
// #pragma GCC target("avx") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) #define FOR(n) for(int i = 0; i < (int)n; i++) #define repi(i,a,b) for(int i = (int)a; i < (int)b; i++) #define all(x) x.begin(),x.end() //#define mp make_pair #define vi vector<int> #define vvi vector<vi> #define vvvi vector<vvi> #define vvvvi vector<vvvi> #define pii pair<int,int> #define vpii vector<pair<int,int>> template<typename T> void chmax(T &a, const T &b) {a = (a > b? a : b);} template<typename T> void chmin(T &a, const T &b) {a = (a < b? a : b);} using ll = long long; using ld = long double; using ull = unsigned long long; const ll INF = numeric_limits<long long>::max() / 2; const ld pi = 3.1415926535897932384626433832795028; const ll mod = 998244353; int dx[] = {1, 0, -1, 0, -1, -1, 1, 1}; int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; #define int long long struct Rolling_Hash { static const unsigned long long rh_mod = (1ull << 61ull) - 1; vector<unsigned long long> power; unsigned long long base; static inline unsigned long long add(unsigned long long a, unsigned long long b) { if((a += b) >= rh_mod) a -= rh_mod; return a; } static inline unsigned long long mul(unsigned long long a, unsigned long long b) { __uint128_t c = (__uint128_t) a * b; return add(c >> 61, c & rh_mod); } static inline unsigned long long generate_base() { mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution< unsigned long long > rand(1, Rolling_Hash::rh_mod - 1); return rand(mt); } inline void expand(size_t sz) { if((int)power.size() < (int)sz + 1) { int pre_sz = (int) power.size(); power.resize(sz + 1); for(int i = pre_sz - 1; i < (int)sz; i++) { power[i + 1] = mul(power[i], base); } } } Rolling_Hash() { base = generate_base(); power = {1}; } vector<unsigned long long> make_hash(const string &s) const { int sz = s.size(); vector<unsigned long long> hashed(sz + 1); for(int i = 0; i < (int)sz; i++) { hashed[i + 1] = add(mul(hashed[i], base), s[i]); } return hashed; } template<typename T> vector<unsigned long long> make_hash(const vector<T> &s) const { int sz = s.size(); vector<unsigned long long> hashed(sz + 1); for(int i = 0; i < (int)sz; i++) { hashed[i + 1] = add(mul(hashed[i], base), s[i]); } return hashed; } vector<unsigned long long> make_hash_string(const string &s) const { vector<char> ss; for(char c : s) ss.push_back(c); return make_hash<char>(ss); } unsigned long long query(const vector<unsigned long long> &s, int l, int r) { expand(r - l); return add(s[r], rh_mod - mul(s[l], power[r - l])); } unsigned long long combine(unsigned long long h1, unsigned long long h2, size_t h2len) { expand(h2len); return add(mul(h1, power[h2len]), h2); } int lcp(const vector<unsigned long long> &a, int l1, int r1, const vector<unsigned long long> &b, int l2, int r2) { int len = min(r1 - l1, r2 - l2); int low = 0, high = len + 1; while(high - low > 1) { int mid = (low + high) / 2; if(query(a, l1, l1 + mid) == query(b, l2, l2 + mid)) low = mid; else high = mid; } return low; } bool issame(const vector<unsigned long long> &a, int l1, int r1, const vector<unsigned long long> &b, int l2, int r2) { assert(r1-l1 == r2-l2); return query(a, l1, r1) == query(b, l2, r2); } bool issame(const vector<unsigned long long> &a, int l1, const vector<unsigned long long> &b, int l2, int len) { return issame(a, l1, l1+len, b, l2, l2+len); } int string_compare(const string &raw1, const vector<unsigned long long> &hash1, int l1, int r1, const string &raw2, const vector<unsigned long long> &hash2, int l2, int r2) { int LCP = lcp(hash1, l1, r1, hash2, l2, r2); if(LCP == min(r1 - l1, r2 - l2)) { if(r1-l1 == r2-l2) return 2; else if(r1-l1 == LCP) return 1; else return 0; } //0: l1の方がでかい, 1: l2の方がでかい return raw1[l1+LCP] < raw2[l2+LCP]; } //0: 1の方がでかい, 1: 2の方がでかい, 2: 同じ template<typename T> int compare(const vector<T> &raw1, const vector<unsigned long long> &hash1, int l1, int r1, const vector<T> &raw2, const vector<unsigned long long> &hash2, int l2, int r2) { int LCP = lcp(hash1, l1, r1, hash2, l2, r2); if(LCP == min(r1 - l1, r2 - l2)) { if(r1-l1 == r2-l2) return 2; else if(r1-l1 == LCP) return 1; else return 0; } //0: l1の方がでかい, 1: l2の方がでかい return raw1[l1+LCP] < raw2[l2+LCP]; } bool is_palindrome(const vector<unsigned long long> normal, const vector<unsigned long long> rev, int l, int r) { const int hash_len = (int)normal.size(); assert((int)rev.size() == hash_len); // cout << l << " " << r << " " << hash_len-r << " " << hash_len-l << endl; return issame(normal, l, r, rev, hash_len-1-r, hash_len-1-l); } }; void solve() { Rolling_Hash hasher; int n; string s; cin >> n >> s; vector<ull> hash = hasher.make_hash_string(s); int ans = 0; FOR(n-1) { if(hasher.string_compare(s, hash, 0, i+1, s, hash, i+1, n) == 1) { ++ans; } } cout << ans << endl; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); int t; cin >> t; while(t--) solve(); return 0; }