program_contest_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ferin-15/program_contest_library

:heavy_check_mark: test/aoj2444.test.cpp

Back to top page

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2444"
#include "../memo/macro.hpp"
#include "../tools/rand.cpp"
#include "../string/rolling_hash.cpp"

signed main(void) {
    ll n, q;
    string s;
    cin >> n >> q >> s;

    rollingHash hash(s);

    ll l = 0, r = 0;
    set<ll> st;
    while(q--) {
        string t;
        cin >> t;
        if(t == "R++") r++;
        else if(t == "R--") r--;
        else if(t == "L++") l++;
        else if(t == "L--") l--;
        st.insert(hash.get(l, r+1));
    }
    cout << st.size() << endl;

    return 0;
}

#line 1 "test/aoj2444.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2444"
#line 1 "memo/macro.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
const ll INF = 1LL<<60;
#line 1 "tools/rand.cpp"
// BEGIN CUT
struct dice {
    mt19937 mt;
    dice() : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
    ll operator()(ll x) { return this->operator()(0, x - 1); }
    ll operator()(ll x, ll y) {
        uniform_int_distribution<ll> dist(x, y);
        return dist(mt);
    }
} rnd;
// END CUT
#line 1 "string/rolling_hash.cpp"
// BEGIN CUT
class rollingHash {
private:
    static constexpr ll mod = (1LL<<61) - 1;
    static ll base;
    vector<ll> hash, p;

    ll mul(ll a, ll b) {
        ll au = a>>31, ad = a & ((1LL<<31)-1);
        ll bu = b>>31, bd = b & ((1LL<<31)-1);
        ll mid = ad*bu+au*bd, midu = mid>>30, midd = mid & ((1LL<<30)-1);
        return au*bu*2 + midu + (midd<<31) + ad*bd;
    }
    ll calcmod(ll x) {
        ll ret = (x>>61) + (x & mod);
        if(ret >= mod) ret -= mod;
        return ret;
    }

public:
    rollingHash(const string &s) : hash(s.size()+1), p(s.size()+1, 1) {
        if(base == -1) base = rnd(2, 100000);
        REP(i, s.size()) {
            hash[i+1] = calcmod(mul(hash[i], base)+s[i]);
            p[i+1] = calcmod(mul(p[i], base));
        }
    }
    // [l,r)
    ll get(int l,int r) {
        return calcmod(hash[r] + 3*mod - mul(hash[l], p[r-l]));
    }
    ll concat(ll h1, ll h2, ll h2len) {
        return calcmod(mul(h1, p[h2len]) + h2);
    }
};
ll rollingHash::base = -1;
// END CUT
#line 5 "test/aoj2444.test.cpp"

signed main(void) {
    ll n, q;
    string s;
    cin >> n >> q >> s;

    rollingHash hash(s);

    ll l = 0, r = 0;
    set<ll> st;
    while(q--) {
        string t;
        cin >> t;
        if(t == "R++") r++;
        else if(t == "R--") r--;
        else if(t == "L++") l++;
        else if(t == "L--") l--;
        st.insert(hash.get(l, r+1));
    }
    cout << st.size() << endl;

    return 0;
}

Back to top page