:heavy_check_mark: test/aoj_2444.test.cpp

Back to top page

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2444"

#include <cstdint>
#include <cstdio>
#include <set>
#include <string>

#include "DataStructure/foldable_deque.cpp"
#include "utility/monoid/rolling_hash.cpp"
#include "ModularArithmetic/modint.cpp"

constexpr intmax_t mod = 998244353;
using mi = modint<mod>;

int main() {
  size_t n, m;
  scanf("%zu %zu", &n, &m);

  char buf[393216];
  scanf("%s", buf);
  std::string s = buf;

  foldable_deque<rolling_hash_monoid<mi>> dq1, dq2;
  std::set<std::pair<intmax_t, intmax_t>> seen;
  size_t il = 0, ir = 0;
  mi b1 = 80067846, b2 = 365378971;

  char c = s[ir++];
  dq1.emplace_back(c, b1);
  dq2.emplace_back(c, b2);
  for (size_t i = 0; i < m; ++i) {
    scanf("%s", buf);
    std::string q = buf;
    if (q == "L++") {
      ++il;
      dq1.pop_front();
      dq2.pop_front();
    } else if (q == "L--") {
      c = s[--il];
      dq1.emplace_front(c, b1);
      dq2.emplace_front(c, b2);
    } else if (q == "R++") {
      c = s[ir++];
      dq1.emplace_back(c, b1);
      dq2.emplace_back(c, b2);
    } else if (q == "R--") {
      --ir;
      dq1.pop_back();
      dq2.pop_back();
    }

    int x1 = dq1.fold().get().get();
    int x2 = dq2.fold().get().get();
    seen.emplace(x1, x2);
  }

  printf("%zu\n", seen.size());
}

#line 1 "test/aoj_2444.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2444"

#include <cstdint>
#include <cstdio>
#include <set>
#include <string>

#line 1 "DataStructure/foldable_deque.cpp"



/**
 * @brief fold 可能両端キュー
 * @author えびちゃん
 */

#include <cstddef>
#include <stack>
#include <utility>

template <typename Monoid>
class foldable_deque {
public:
  using size_type = size_t;
  using value_type = Monoid;

private:
  std::stack<value_type> M_front, M_back;
  std::stack<value_type> M_front_folded, M_back_folded;

  void M_rotate_to_front() {
    // precondition: M_front.empty(), M_back.size() == n > 0
    // postcondition: M_front.size() == (n+1)/2, M_back.size() == n/2
    size_type n = M_back.size();
    std::stack<value_type>().swap(M_back_folded);  // clear
    std::stack<value_type> tmp;
    for (size_type i = 0; i < n/2; ++i) {
      tmp.push(std::move(M_back.top()));
      M_back.pop();
    }
    while (!M_back.empty()) {
      push_front(M_back.top());
      M_back.pop();
    }
    while (!tmp.empty()) {
      push_back(tmp.top());
      tmp.pop();
    }
  }

  void M_rotate_to_back() {
    // precondition: M_front.size() == n > 0, M_back.empty()
    // postcondition: M_front.size() == n/2, M_back.size() == (n+1)/2
    size_type n = M_front.size();
    std::stack<value_type>().swap(M_front_folded);  // clear
    std::stack<value_type> tmp;
    for (size_type i = 0; i < n/2; ++i) {
      tmp.push(std::move(M_front.top()));
      M_front.pop();
    }
    while (!M_front.empty()) {
      push_back(M_front.top());
      M_front.pop();
    }
    while (!tmp.empty()) {
      push_front(tmp.top());
      tmp.pop();
    }
  }

public:
  size_type size() const { return M_front.size() + M_back.size(); }
  bool empty() const noexcept { return M_front.empty() && M_back.empty(); }

  void push_back(value_type const& x) {
    M_back.push(x);
    value_type f = (M_back_folded.empty()? x: M_back_folded.top() + x);
    M_back_folded.push(f);
  }
  void push_front(value_type const& x) {
    M_front.push(x);
    value_type f = (M_front_folded.empty()? x: x + M_front_folded.top());
    M_front_folded.push(f);
  }
  template <typename... Args>
  void emplace_back(Args&&... args) {
    M_back.emplace(std::forward<Args>(args)...);
    value_type f = (M_back_folded.empty()? M_back.top(): M_back_folded.top() + M_back.top());
    M_back_folded.push(f);
  }
  template <typename... Args>
  void emplace_front(Args&&... args) {
    M_front.emplace(std::forward<Args>(args)...);
    value_type f = (M_front_folded.empty()? M_front.top(): M_front.top() + M_front_folded.top());
    M_front_folded.push(f);
  }

  void pop_back() {
    if (M_back.empty()) M_rotate_to_back();
    M_back.pop();
    M_back_folded.pop();
  }
  void pop_front() {
    if (M_front.empty()) M_rotate_to_front();
    M_front.pop();
    M_front_folded.pop();
  }

  value_type fold() const {
    value_type res{};
    if (!M_front_folded.empty()) res += M_front_folded.top();
    if (!M_back_folded.empty()) res += M_back_folded.top();
    return res;
  }
};


#line 1 "utility/monoid/rolling_hash.cpp"



/**
 * @brief ロリハの演算のモノイド
 * @author えびちゃん
 */

#line 10 "utility/monoid/rolling_hash.cpp"

template <typename ModInt>
class rolling_hash_monoid {
public:
  using value_type = ModInt;

private:
  value_type M_x = 0, M_p = 1;

public:
  rolling_hash_monoid() = default;  // identity

  rolling_hash_monoid(value_type const& x, value_type const& p): M_x(x), M_p(p) {};

  rolling_hash_monoid& operator +=(rolling_hash_monoid const& that) {
    M_x = M_x * that.M_p + that.M_x;
    M_p *= that.M_p;
    return *this;
  }
  friend bool operator ==(rolling_hash_monoid const& lhs, rolling_hash_monoid const& rhs) {
    return lhs.M_x == rhs.M_x && lhs.M_p == rhs.M_p;
  }

  friend rolling_hash_monoid operator +(rolling_hash_monoid lhs, rolling_hash_monoid const& rhs) {
    return lhs += rhs;
  }
  friend bool operator !=(rolling_hash_monoid const& lhs, rolling_hash_monoid const& rhs) {
    return !(lhs == rhs);
  }

  value_type const& get() const { return M_x; }
  value_type const& coefficient() const { return M_p; }
};


#line 1 "ModularArithmetic/modint.cpp"



/**
 * @brief 合同算術用クラス
 * @author えびちゃん
 */

#line 10 "ModularArithmetic/modint.cpp"
#include <limits>
#include <type_traits>
#line 13 "ModularArithmetic/modint.cpp"

template <intmax_t Modulo>
class modint {
public:
  using value_type = typename std::conditional<
  (0 < Modulo && Modulo < std::numeric_limits<int>::max() / 2), int, intmax_t
 >::type;

private:
  static constexpr value_type S_cmod = Modulo;  // compile-time
  static value_type S_rmod;  // runtime
  value_type M_value = 0;

  static constexpr value_type S_inv(value_type n, value_type m) {
    value_type x = 0;
    value_type y = 1;
    value_type a = n;
    value_type b = m;
    for (value_type u = y, v = x; a;) {
      value_type q = b / a;
      std::swap(x -= q*u, u);
      std::swap(y -= q*v, v);
      std::swap(b -= q*a, a);
    }
    if ((x %= m) < 0) x += m;
    return x;
  }

  static value_type S_normalize(intmax_t n, value_type m) {
    if (n >= m) {
      n %= m;
    } else if (n < 0) {
      if ((n %= m) < 0) n += m;
    }
    return n;
  }

public:
  modint() = default;
  modint(intmax_t n): M_value(S_normalize(n, get_modulo())) {}

  modint& operator =(intmax_t n) {
    M_value = S_normalize(n, get_modulo());
    return *this;
  }

  modint& operator +=(modint const& that) {
    if ((M_value += that.M_value) >= get_modulo()) M_value -= get_modulo();
    return *this;
  }
  modint& operator -=(modint const& that) {
    if ((M_value -= that.M_value) < 0) M_value += get_modulo();
    return *this;
  }
  modint& operator *=(modint const& that) {
    intmax_t tmp = M_value;
    tmp *= that.M_value;
    M_value = tmp % get_modulo();
    return *this;
  }
  modint& operator /=(modint const& that) {
    intmax_t tmp = M_value;
    tmp *= S_inv(that.M_value, get_modulo());
    M_value = tmp % get_modulo();
    return *this;
  }

  modint& operator ++() {
    if (++M_value == get_modulo()) M_value = 0;
    return *this;
  }
  modint& operator --() {
    if (M_value-- == 0) M_value = get_modulo()-1;
    return *this;
  }

  modint operator ++(int) { modint tmp(*this); ++*this; return tmp; }
  modint operator --(int) { modint tmp(*this); --*this; return tmp; }

  friend modint operator +(modint lhs, modint const& rhs) { return lhs += rhs; }
  friend modint operator -(modint lhs, modint const& rhs) { return lhs -= rhs; }
  friend modint operator *(modint lhs, modint const& rhs) { return lhs *= rhs; }
  friend modint operator /(modint lhs, modint const& rhs) { return lhs /= rhs; }

  modint operator +() const { return *this; }
  modint operator -() const {
    if (M_value == 0) return *this;
    return modint(get_modulo() - M_value);
  }

  friend bool operator ==(modint const& lhs, modint const& rhs) {
    return lhs.M_value == rhs.M_value;
  }
  friend bool operator !=(modint const& lhs, modint const& rhs) {
    return !(lhs == rhs);
  }

  value_type get() const { return M_value; }
  static value_type get_modulo() { return ((S_cmod > 0)? S_cmod: S_rmod); }

  template <int M = Modulo, typename Tp = typename std::enable_if<(M <= 0)>::type>
  static Tp set_modulo(value_type m) { S_rmod = m; }
};

template <intmax_t N>
constexpr typename modint<N>::value_type modint<N>::S_cmod;
template <intmax_t N>
typename modint<N>::value_type modint<N>::S_rmod;


#line 11 "test/aoj_2444.test.cpp"

constexpr intmax_t mod = 998244353;
using mi = modint<mod>;

int main() {
  size_t n, m;
  scanf("%zu %zu", &n, &m);

  char buf[393216];
  scanf("%s", buf);
  std::string s = buf;

  foldable_deque<rolling_hash_monoid<mi>> dq1, dq2;
  std::set<std::pair<intmax_t, intmax_t>> seen;
  size_t il = 0, ir = 0;
  mi b1 = 80067846, b2 = 365378971;

  char c = s[ir++];
  dq1.emplace_back(c, b1);
  dq2.emplace_back(c, b2);
  for (size_t i = 0; i < m; ++i) {
    scanf("%s", buf);
    std::string q = buf;
    if (q == "L++") {
      ++il;
      dq1.pop_front();
      dq2.pop_front();
    } else if (q == "L--") {
      c = s[--il];
      dq1.emplace_front(c, b1);
      dq2.emplace_front(c, b2);
    } else if (q == "R++") {
      c = s[ir++];
      dq1.emplace_back(c, b1);
      dq2.emplace_back(c, b2);
    } else if (q == "R--") {
      --ir;
      dq1.pop_back();
      dq2.pop_back();
    }

    int x1 = dq1.fold().get().get();
    int x2 = dq2.fold().get().get();
    seen.emplace(x1, x2);
  }

  printf("%zu\n", seen.size());
}

Back to top page