結果
問題 | No.2454 Former < Latter |
ユーザー | rniya |
提出日時 | 2023-09-01 21:51:41 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 470 ms / 2,000 ms |
コード長 | 3,702 bytes |
コンパイル時間 | 2,235 ms |
コンパイル使用メモリ | 204,896 KB |
実行使用メモリ | 7,928 KB |
最終ジャッジ日時 | 2023-09-01 21:51:47 |
合計ジャッジ時間 | 4,686 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
4,376 KB |
testcase_01 | AC | 2 ms
4,384 KB |
testcase_02 | AC | 49 ms
7,848 KB |
testcase_03 | AC | 46 ms
7,832 KB |
testcase_04 | AC | 43 ms
7,772 KB |
testcase_05 | AC | 45 ms
7,420 KB |
testcase_06 | AC | 40 ms
6,636 KB |
testcase_07 | AC | 41 ms
6,872 KB |
testcase_08 | AC | 21 ms
4,376 KB |
testcase_09 | AC | 24 ms
4,380 KB |
testcase_10 | AC | 27 ms
4,376 KB |
testcase_11 | AC | 45 ms
7,828 KB |
testcase_12 | AC | 45 ms
7,928 KB |
testcase_13 | AC | 45 ms
7,856 KB |
testcase_14 | AC | 46 ms
7,848 KB |
testcase_15 | AC | 35 ms
5,512 KB |
testcase_16 | AC | 35 ms
5,128 KB |
testcase_17 | AC | 470 ms
4,380 KB |
testcase_18 | AC | 24 ms
4,380 KB |
testcase_19 | AC | 40 ms
6,864 KB |
testcase_20 | AC | 43 ms
6,800 KB |
testcase_21 | AC | 26 ms
4,380 KB |
testcase_22 | AC | 26 ms
4,376 KB |
testcase_23 | AC | 26 ms
4,380 KB |
ソースコード
#include <bits/stdc++.h> #ifdef LOCAL #include <debug.hpp> #else #define debug(...) void(0) #endif struct RollingHash { static inline uint64_t generate_base() { std::mt19937_64 mt(std::chrono::steady_clock::now().time_since_epoch().count()); std::uniform_int_distribution<uint64_t> rand(2, RollingHash::mod - 1); return rand(mt); } RollingHash(uint64_t base = generate_base()) : base(base), power{1} {} template <typename T> std::vector<uint64_t> build(const T& s) const { int n = s.size(); std::vector<uint64_t> hash(n + 1); hash[0] = 0; for (int i = 0; i < n; i++) hash[i + 1] = add(mul(hash[i], base), s[i]); return hash; } template <typename T> uint64_t get(const T& s) const { uint64_t res = 0; for (const auto& x : s) res = add(mul(res, base), x); return res; } uint64_t query(const std::vector<uint64_t>& hash, int l, int r) { assert(0 <= l && l <= r); extend(r - l); return add(hash[r], mod - mul(hash[l], power[r - l])); } uint64_t combine(uint64_t h1, uint64_t h2, size_t h2_len) { extend(h2_len); return add(mul(h1, power[h2_len]), h2); } int lcp(const std::vector<uint64_t>& a, int l1, int r1, const std::vector<uint64_t>& b, int l2, int r2) { int len = std::min(r1 - l1, r2 - l2); int lb = 0, ub = len + 1; while (ub - lb > 1) { int mid = (lb + ub) >> 1; (query(a, l1, l1 + mid) == query(b, l2, l2 + mid) ? lb : ub) = mid; } return lb; } private: static constexpr uint64_t mod = (1ULL << 61) - 1; const uint64_t base; std::vector<uint64_t> power; static inline uint64_t add(uint64_t a, uint64_t b) { if ((a += b) >= mod) a -= mod; return a; } static inline uint64_t mul(uint64_t a, uint64_t b) { __uint128_t c = (__uint128_t)a * b; return add(c >> 61, c & mod); } inline void extend(size_t len) { if (power.size() > len) return; size_t pre = power.size(); power.resize(len + 1); for (size_t i = pre - 1; i < len; i++) power[i + 1] = mul(power[i], base); } }; using namespace std; typedef long long ll; #define all(x) begin(x), end(x) constexpr int INF = (1 << 30) - 1; constexpr long long IINF = (1LL << 60) - 1; constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; template <class T> istream& operator>>(istream& is, vector<T>& v) { for (auto& x : v) is >> x; return is; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { auto sep = ""; for (const auto& x : v) os << exchange(sep, " ") << x; return os; } template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); } template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); } template <class T> void mkuni(vector<T>& v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); } void solve() { int N; string S; cin >> N >> S; RollingHash rh; auto hash = rh.build(S); int ans = 0; for (int i = 1; i < N; i++) { int len = rh.lcp(hash, 0, i, hash, i, N); if (len < i and len < N - i) ans += (S[len] < S[i + len]); else if (len == i and len < N - i) ans++; } cout << ans << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; for (; T--;) solve(); return 0; }