結果
問題 | No.2454 Former < Latter |
ユーザー | rniya |
提出日時 | 2023-09-01 21:51:41 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 252 ms / 2,000 ms |
コード長 | 3,702 bytes |
コンパイル時間 | 2,552 ms |
コンパイル使用メモリ | 198,640 KB |
実行使用メモリ | 8,124 KB |
最終ジャッジ日時 | 2025-01-03 08:17:12 |
合計ジャッジ時間 | 5,007 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 23 |
ソースコード
#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; }