This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ebi-fly13/Library
#define PROBLEM \ "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2444&lang=jp" #include <cassert> #include <cstdint> #include <iostream> #include <set> #include <string> #include <vector> #include "../../string/rolling_hash.hpp" #include "../../utility/hash.hpp" using u64 = std::uint64_t; int main() { int n, m; std::cin >> n >> m; std::string s; std::cin >> s; std::set<ebi::Hash<2>> set; ebi::rolling_hash<2>::set_base(); ebi::rolling_hash<2> rh(s); int l = 0; int r = 1; while (m--) { std::string q; std::cin >> q; if (q == "L++") { l++; } else if (q == "L--") { l--; } else if (q == "R++") { r++; } else if (q == "R--") { r--; } else { assert(0); } set.insert(rh.get_hash(l, r)); } std::cout << int(set.size()) << '\n'; }
#line 1 "test/aoj/aoj_2444.test.cpp" #define PROBLEM \ "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2444&lang=jp" #include <cassert> #include <cstdint> #include <iostream> #include <set> #include <string> #include <vector> #line 2 "string/rolling_hash.hpp" #include <array> #line 7 "string/rolling_hash.hpp" #line 2 "modint/modint61.hpp" #line 6 "modint/modint61.hpp" #line 2 "modint/base.hpp" #include <concepts> #line 5 "modint/base.hpp" #include <utility> namespace ebi { template <class T> concept Modint = requires(T a, T b) { a + b; a - b; a * b; a / b; a.inv(); a.val(); a.pow(std::declval<long long>()); T::mod(); }; template <Modint mint> std::istream &operator>>(std::istream &os, mint &a) { long long x; os >> x; a = x; return os; } template <Modint mint> std::ostream &operator<<(std::ostream &os, const mint &a) { return os << a.val(); } } // namespace ebi #line 8 "modint/modint61.hpp" namespace ebi { struct modint61 { private: using mint = modint61; using u64 = std::uint64_t; constexpr static u64 m = (1ull << 61) - 1; constexpr static u64 MASK31 = (1ull << 31) - 1; constexpr static u64 MASK30 = (1ull << 30) - 1; public: constexpr static u64 mod() { return m; } constexpr modint61() : _v(0) {} constexpr modint61(long long v) { v %= (long long)umod(); if (v < 0) v += (long long)umod(); _v = u64(v); } constexpr u64 val() const { return _v; } constexpr u64 value() const { return val(); } constexpr mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } constexpr mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } constexpr mint &operator+=(const mint &rhs) { _v += rhs._v; _v = safe_mod(_v); return *this; } constexpr mint &operator-=(const mint &rhs) { if (_v < rhs._v) _v += umod(); assert(_v >= rhs._v); _v -= rhs._v; return *this; } constexpr mint &operator*=(const mint &rhs) { u64 au = _v >> 31, ad = _v & MASK31; u64 bu = rhs._v >> 31, bd = rhs._v & MASK31; u64 mid = ad * bu + au * bd; u64 midu = mid >> 30; u64 midd = mid & MASK30; _v = (au * bu * 2 + midu + (midd << 31) + ad * bd); _v = safe_mod(_v); return *this; } constexpr mint &operator/=(const mint &rhs) { return *this *= rhs.inv(); } constexpr mint pow(long long n) const { assert(0 <= n); mint x = *this, res = 1; while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } constexpr mint inv() const { assert(_v); return pow(umod() - 2); } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs.val() == rhs.val(); } friend bool operator!=(const mint &lhs, const mint &rhs) { return !(lhs == rhs); } friend bool operator<(const mint &lhs, const mint &rhs) { return lhs._v < rhs._v; } friend bool operator>(const mint &lhs, const mint &rhs) { return rhs < lhs; } private: u64 _v = 0; constexpr static u64 umod() { return m; } constexpr u64 safe_mod(const u64 &a) { u64 au = a >> 61; u64 ad = a & umod(); u64 res = au + ad; if (res >= umod()) res -= umod(); return res; } }; } // namespace ebi #line 2 "utility/hash.hpp" #line 4 "utility/hash.hpp" #line 2 "utility/random_number_generator.hpp" #line 4 "utility/random_number_generator.hpp" #include <random> namespace ebi { struct random_number_generator { random_number_generator(int seed = -1) { if (seed < 0) seed = rnd(); mt.seed(seed); } void set_seed(int seed) { mt.seed(seed); } template <class T> T get(T a, T b) { std::uniform_int_distribution<T> dist(a, b - 1); return dist(mt); } private: std::mt19937_64 mt; std::random_device rnd; }; } // namespace ebi #line 7 "utility/hash.hpp" namespace ebi { template <int BASE_NUM = 2> struct Hash : std::array<modint61, BASE_NUM> { private: using std::array<modint61, BASE_NUM>::array; using std::array<modint61, BASE_NUM>::operator=; public: Hash() : std::array<modint61, BASE_NUM>() {} constexpr static Hash set(const modint61 &a) { Hash res; std::fill(res.begin(), res.end(), a); return res; } constexpr Hash &operator+=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] += rhs[i]; } return *this; } constexpr Hash &operator-=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] -= rhs[i]; } return *this; } constexpr Hash &operator*=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] *= rhs[i]; } return *this; } constexpr Hash &operator+=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] += rhs; } return *this; } constexpr Hash &operator-=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] -= rhs; } return *this; } constexpr Hash &operator*=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] *= rhs; } return *this; } Hash operator+(const Hash &rhs) const { return Hash(*this) += rhs; } Hash operator-(const Hash &rhs) const { return Hash(*this) -= rhs; } Hash operator*(const Hash &rhs) const { return Hash(*this) *= rhs; } Hash operator+(const modint61 &rhs) const { return Hash(*this) += rhs; } Hash operator-(const modint61 &rhs) const { return Hash(*this) -= rhs; } Hash operator*(const modint61 &rhs) const { return Hash(*this) *= rhs; } Hash pow(long long n) const { Hash a = *this, res = set(1); while (n) { if (n & 1) res *= a; a *= a; n >>= 1; } return res; } static Hash get_basis() { static random_number_generator rng; Hash h; for (int i = 0; i < BASE_NUM; i++) { h[i] = rng.get<std::uint64_t>(0, modint61::mod() - 1) + 1; } return h; } Hash inv() const { Hash h; for (int i = 0; i < BASE_NUM; i++) { h[i] = (*this)[i].inv(); } return h; } static Hash get_basis_primitive() { static random_number_generator rng; Hash h; for (int i = 0; i < BASE_NUM; i++) { while (!is_primitive( (h[i] = rng.get<std::uint64_t>(0, modint61::mod() - 1) + 1) .val())) ; } return h; } private: static bool is_primitive(long long x) { for (long long d : {2, 3, 5, 7, 11, 13, 31, 41, 61, 151, 331, 1321}) { if (modint61(x).pow((modint61::mod() - 1) / d).val() <= 1) return false; } return true; } }; } // namespace ebi #line 10 "string/rolling_hash.hpp" /* reference: https://qiita.com/keymoon/items/11fac5627672a6d6a9f6 */ namespace ebi { template <int n = 2> struct rolling_hash { private: static constexpr int h = 100; using Self = rolling_hash<n>; static void expand(int m) { int now = base_pow.size(); while (now <= m) { now <<= 1; } if (int(base_pow.size()) == now) return; base_pow.reserve(now); while (int(base_pow.size()) < now) { base_pow.emplace_back(base_pow.back() * base); } } public: rolling_hash(const std::string &s) : sz(s.size()) { assert(n >= 0); hash.reserve(sz + 1); hash.emplace_back(Hash<n>::set(0)); expand(sz); for (const auto &c : s) { hash.emplace_back(hash.back() * base + c + h); } } template <class T> rolling_hash(const std::vector<T> &a) : sz(a.size()) { assert(n >= 0); hash.reserve(sz + 1); hash.emplace_back(Hash<n>::set(0)); expand(sz); for (const auto &c : a) { hash.emplace_back(hash.back() * base + c + h); } } inline Hash<n> prefix_hash(int r) const { return hash[r]; } // [l, r) Hash<n> get_hash(int l, int r) const { assert(0 <= l && l <= r && r <= sz); return prefix_hash(r) - prefix_hash(l) * base_pow[r - l]; } static Hash<n> get_hash(const std::string &str, int l = 0, int r = -1) { if (r < 0) r = int(str.size()); Hash<n> res = Hash<n>::set(0); for (int i = l; i < r; i++) { res = res * base + str[i] + h; } return res; } template <class T> static Hash<n> get_hash(const std::vector<T> &a, int l = 0, int r = -1) { if (r < 0) r = int(a.size()); Hash<n> res = Hash<n>::set(0); for (int i = l; i < r; i++) { res = res * base + a[i] + h; } return res; } static Hash<n> concat(Self lhs, Self rhs) { return lhs.hash.back() * base_pow[rhs.size()] + rhs.hash.back(); } static Hash<n> concat(Hash<n> lhs, Hash<n> rhs, int len) { return lhs * base_pow[len] + rhs; } int size() const { return sz; } Self operator+(const Self &rhs) noexcept { return Self(*this) += rhs; } Self &operator+=(const Self &rhs) noexcept { Hash<n> a = hash.back(); for (int i = 1; i <= rhs.size(); i++) { a *= base; hash.emplace_back(a + rhs.hash[i]); } sz += rhs.size(); expand(sz); return *this; } static void set_base() { base = Hash<n>::get_basis_primitive(); base_pow = std::vector<Hash<n>>(1, Hash<n>::set(1)); } static Hash<n> get_base() { return base; } private: int sz; std::vector<Hash<n>> hash; static Hash<n> base; static std::vector<Hash<n>> base_pow; }; template <int n> Hash<n> rolling_hash<n>::base = {}; template <int n> std::vector<Hash<n>> rolling_hash<n>::base_pow = {}; } // namespace ebi #line 13 "test/aoj/aoj_2444.test.cpp" using u64 = std::uint64_t; int main() { int n, m; std::cin >> n >> m; std::string s; std::cin >> s; std::set<ebi::Hash<2>> set; ebi::rolling_hash<2>::set_base(); ebi::rolling_hash<2> rh(s); int l = 0; int r = 1; while (m--) { std::string q; std::cin >> q; if (q == "L++") { l++; } else if (q == "L--") { l--; } else if (q == "R++") { r++; } else if (q == "R--") { r--; } else { assert(0); } set.insert(rh.get_hash(l, r)); } std::cout << int(set.size()) << '\n'; }