This documentation is automatically generated by online-judge-tools/verification-helper
#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"
int main() {
int n, m;
std::cin >> n >> m;
std::string s;
std::cin >> s;
ebi::rolling_hash rh(s);
std::set<ebi::rolling_hash_monoid> set;
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"
#line 5 "string/rolling_hash.hpp"
#line 2 "monoid/rolling_hash_monoid.hpp"
#include <utility>
#line 2 "modint/modint61.hpp"
#line 6 "modint/modint61.hpp"
#line 2 "modint/base.hpp"
#include <concepts>
#line 6 "modint/base.hpp"
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/random_number_generator.hpp"
#include <algorithm>
#line 5 "utility/random_number_generator.hpp"
#include <numeric>
#include <random>
#line 8 "utility/random_number_generator.hpp"
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);
}
std::vector<int> get_permutation(int n) {
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 0);
std::shuffle(p.begin(), p.end(), mt);
return p;
}
private:
std::mt19937_64 mt;
std::random_device rnd;
};
} // namespace ebi
#line 7 "monoid/rolling_hash_monoid.hpp"
namespace ebi {
struct rolling_hash_monoid : std::pair<modint61, modint61> {
private:
using Self = rolling_hash_monoid;
using std::pair<modint61, modint61>::pair;
public:
template <class T> rolling_hash_monoid(T x) {
this->first = get_base();
this->second = x;
}
modint61 get_hash() {
return this->second;
}
Self &operator+=(const Self &rhs) {
this->first = this->first * rhs.first;
this->second = this->second * rhs.first + rhs.second;
return *this;
}
friend Self operator+(const Self &lhs, const Self &rhs) {
return Self(lhs) += rhs;
}
static Self e() {
return {1, 0};
}
static modint61 get_base() {
static modint61 base = 0;
static random_number_generator rng;
while (base == 0) {
base = rng.get<std::uint64_t>(2, modint61::mod());
}
return base;
}
};
} // namespace ebi
#line 7 "string/rolling_hash.hpp"
/*
reference: https://qiita.com/keymoon/items/11fac5627672a6d6a9f6
*/
namespace ebi {
struct rolling_hash {
private:
public:
rolling_hash(const std::string &s) {
prefix_hash.emplace_back(rolling_hash_monoid::e());
for (auto c : s) {
prefix_hash.emplace_back(prefix_hash.back() + c);
}
}
template <class T> rolling_hash(const std::vector<T> &a) {
prefix_hash.emplace_back(rolling_hash_monoid::e());
for (auto x : a) {
prefix_hash.emplace_back(prefix_hash.back() + x);
}
}
rolling_hash_monoid get_prefix_hash(int r) const {
return prefix_hash[r];
}
rolling_hash_monoid get_hash(int l, int r) const {
assert(l <= r && r <= (int)prefix_hash.size());
modint61 base_pow = prefix_hash[r - l].first;
return {base_pow,
prefix_hash[r].second - prefix_hash[l].second * base_pow};
}
rolling_hash &operator+=(const rolling_hash &rhs) noexcept {
rolling_hash_monoid lhs = prefix_hash.back();
for (int i = 1; i <= (int)rhs.prefix_hash.size(); i++) {
prefix_hash.emplace_back(lhs + rhs.prefix_hash[i]);
}
return *this;
}
rolling_hash operator+(const rolling_hash &rhs) noexcept {
return rolling_hash(*this) += rhs;
}
private:
std::vector<rolling_hash_monoid> prefix_hash;
};
} // namespace ebi
#line 12 "test/aoj/aoj_2444.test.cpp"
int main() {
int n, m;
std::cin >> n >> m;
std::string s;
std::cin >> s;
ebi::rolling_hash rh(s);
std::set<ebi::rolling_hash_monoid> set;
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';
}