This documentation is automatically generated by online-judge-tools/verification-helper
#include "../../number/modint_mersenne61.hpp"
#include "../rolling_hash_1d.hpp"
#include <iostream>
#include <string>
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_14_B"
using namespace std;
int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);
string T, P;
cin >> T >> P;
rolling_hash<ModIntMersenne61> rh_T(T), rh_P(P);
for (int l = 0; l < (int)(T.length() - P.length() + 1); l++) {
if (rh_T.get(l, l + P.length()) == rh_P.get(0, P.length())) cout << l << '\n';
}
}
#line 2 "number/modint_mersenne61.hpp"
#include <cassert>
#include <chrono>
#include <random>
// F_p, p = 2^61 - 1
// https://qiita.com/keymoon/items/11fac5627672a6d6a9f6
class ModIntMersenne61 {
static const long long md = (1LL << 61) - 1;
long long _v;
inline unsigned hi() const noexcept { return _v >> 31; }
inline unsigned lo() const noexcept { return _v & ((1LL << 31) - 1); }
public:
static long long mod() { return md; }
ModIntMersenne61() : _v(0) {}
// 0 <= x < md * 2
explicit ModIntMersenne61(long long x) : _v(x >= md ? x - md : x) {
assert(0 <= x and x < md * 2);
}
long long val() const noexcept { return _v; }
ModIntMersenne61 operator+(const ModIntMersenne61 &x) const {
return ModIntMersenne61(_v + x._v);
}
ModIntMersenne61 operator-(const ModIntMersenne61 &x) const {
return ModIntMersenne61(_v + md - x._v);
}
ModIntMersenne61 operator*(const ModIntMersenne61 &x) const {
using ull = unsigned long long;
ull uu = (ull)hi() * x.hi() * 2;
ull ll = (ull)lo() * x.lo();
ull lu = (ull)hi() * x.lo() + (ull)lo() * x.hi();
ull sum = uu + ll + ((lu & ((1ULL << 30) - 1)) << 31) + (lu >> 30);
ull reduced = (sum >> 61) + (sum & ull(md));
return ModIntMersenne61(reduced);
}
ModIntMersenne61 pow(long long n) const {
assert(n >= 0);
ModIntMersenne61 ans(1), tmp = *this;
while (n) {
if (n & 1) ans *= tmp;
tmp *= tmp, n >>= 1;
}
return ans;
}
ModIntMersenne61 inv() const { return pow(md - 2); }
ModIntMersenne61 operator/(const ModIntMersenne61 &x) const { return *this * x.inv(); }
ModIntMersenne61 operator-() const { return ModIntMersenne61(md - _v); }
ModIntMersenne61 &operator+=(const ModIntMersenne61 &x) { return *this = *this + x; }
ModIntMersenne61 &operator-=(const ModIntMersenne61 &x) { return *this = *this - x; }
ModIntMersenne61 &operator*=(const ModIntMersenne61 &x) { return *this = *this * x; }
ModIntMersenne61 &operator/=(const ModIntMersenne61 &x) { return *this = *this / x; }
ModIntMersenne61 operator+(unsigned x) const { return ModIntMersenne61(this->_v + x); }
bool operator==(const ModIntMersenne61 &x) const { return _v == x._v; }
bool operator!=(const ModIntMersenne61 &x) const { return _v != x._v; }
bool operator<(const ModIntMersenne61 &x) const { return _v < x._v; } // To use std::map
template <class OStream> friend OStream &operator<<(OStream &os, const ModIntMersenne61 &x) {
return os << x._v;
}
static ModIntMersenne61 randgen(bool force_update = false) {
static ModIntMersenne61 b(0);
if (b == ModIntMersenne61(0) or force_update) {
std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count());
std::uniform_int_distribution<long long> d(1, ModIntMersenne61::mod());
b = ModIntMersenne61(d(mt));
}
return b;
}
};
#line 2 "string/rolling_hash_1d.hpp"
#include <algorithm>
#line 5 "string/rolling_hash_1d.hpp"
#include <string>
#include <tuple>
#include <vector>
template <class T1, class T2> struct PairHash : public std::pair<T1, T2> {
using PH = PairHash<T1, T2>;
explicit PairHash(T1 x, T2 y) : std::pair<T1, T2>(x, y) {}
explicit PairHash(int x) : std::pair<T1, T2>(x, x) {}
PairHash() : PairHash(0) {}
PH operator+(const PH &x) const { return PH(this->first + x.first, this->second + x.second); }
PH operator-(const PH &x) const { return PH(this->first - x.first, this->second - x.second); }
PH operator*(const PH &x) const { return PH(this->first * x.first, this->second * x.second); }
PH operator+(int x) const { return PH(this->first + x, this->second + x); }
static PH randgen(bool force_update = false) {
static PH b(0);
if (b == PH(0) or force_update) {
std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count());
std::uniform_int_distribution<int> d(1 << 30);
b = PH(T1(d(mt)), T2(d(mt)));
}
return b;
}
template <class OStream> friend OStream &operator<<(OStream &os, const PH &x) {
return os << "(" << x.first << ", " << x.second << ")";
}
};
template <class T1, class T2, class T3> struct TupleHash3 : public std::tuple<T1, T2, T3> {
using TH = TupleHash3<T1, T2, T3>;
explicit TupleHash3(T1 x, T2 y, T3 z) : std::tuple<T1, T2, T3>(x, y, z) {}
explicit TupleHash3(int x) : std::tuple<T1, T2, T3>(x, x, x) {}
TupleHash3() : TupleHash3(0) {}
inline const T1 &v1() const noexcept { return std::get<0>(*this); }
inline const T2 &v2() const noexcept { return std::get<1>(*this); }
inline const T3 &v3() const noexcept { return std::get<2>(*this); }
TH operator+(const TH &x) const { return TH(v1() + x.v1(), v2() + x.v2(), v3() + x.v3()); }
TH operator-(const TH &x) const { return TH(v1() - x.v1(), v2() - x.v2(), v3() - x.v3()); }
TH operator*(const TH &x) const { return TH(v1() * x.v1(), v2() * x.v2(), v3() * x.v3()); }
TH operator+(int x) const { return TH(v1() + x, v2() + x, v3() + x); }
static TH randgen(bool force_update = false) {
static TH b(0);
if (b == TH(0) or force_update) {
std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count());
std::uniform_int_distribution<int> d(1 << 30);
b = TH(T1(d(mt)), T2(d(mt)), T3(d(mt)));
}
return b;
}
template <class OStream> friend OStream &operator<<(OStream &os, const TH &x) {
return os << "(" << x.v1() << ", " << x.v2() << ", " << x.v3() << ")";
}
};
// Rolling Hash (Rabin-Karp), 1dim
template <typename V> struct rolling_hash {
int N;
const V B;
std::vector<V> hash; // hash[i] = s[0] * B^(i - 1) + ... + s[i - 1]
static std::vector<V> power; // power[i] = B^i
void _extend_powvec() {
if (power.size() > 1 and power.at(1) != B) power = {V(1)};
while (static_cast<int>(power.size()) <= N) {
auto tmp = power.back() * B;
power.push_back(tmp);
}
}
template <typename Int>
rolling_hash(const std::vector<Int> &s, V b = V::randgen()) : N(s.size()), B(b), hash(N + 1) {
for (int i = 0; i < N; i++) hash[i + 1] = hash[i] * B + s[i];
_extend_powvec();
}
rolling_hash(const std::string &s = "", V b = V::randgen()) : N(s.size()), B(b), hash(N + 1) {
for (int i = 0; i < N; i++) hash[i + 1] = hash[i] * B + s[i];
_extend_powvec();
}
void addchar(const char &c) {
V hnew = hash[N] * B + c;
N++, hash.emplace_back(hnew);
_extend_powvec();
}
struct Hash {
int length;
V val;
Hash() : length(0), val(V()) {}
Hash(int len, const V &v) : length(len), val(v) {}
bool operator==(const Hash &r) const noexcept {
return length == r.length and val == r.val;
}
bool operator<(const Hash &x) const { // To use std::map
if (length != x.length) return length < x.length;
return val < x.val;
}
Hash operator*(const Hash &r) const {
return Hash(length + r.length, val * power.at(r.length) + r.val);
}
template <class OStream> friend OStream &operator<<(OStream &os, const Hash &x) {
return os << "(length=" << x.length << ", val=" << x.val << ")";
}
};
Hash get(int l, int r) const { // s[l] * B^(r - l - 1) + ... + s[r - 1]
if (l >= r) return Hash();
return Hash(r - l, hash[r] - hash[l] * power[r - l]);
}
int lcplen(int l1, int l2) const { return longest_common_prefix(*this, l1, *this, l2); }
};
template <typename V> std::vector<V> rolling_hash<V>::power{V(1)};
// Longest common prerfix between s1[l1, N1) and s2[l2, N2)
template <typename T>
int longest_common_prefix(const rolling_hash<T> &rh1, int l1, const rolling_hash<T> &rh2, int l2) {
int lo = 0, hi = std::min(rh1.N + 1 - l1, rh2.N + 1 - l2);
while (hi - lo > 1) {
const int c = (lo + hi) / 2;
auto h1 = rh1.get(l1, l1 + c), h2 = rh2.get(l2, l2 + c);
(h1 == h2 ? lo : hi) = c;
}
return lo;
}
// Longest common suffix between s1[0, r1) and s2[0, r2)
template <typename T>
int longest_common_suffix(const rolling_hash<T> &rh1, int r1, const rolling_hash<T> &rh2, int r2) {
int lo = 0, hi = std::min(r1, r2) + 1;
while (hi - lo > 1) {
const int c = (lo + hi) / 2;
auto h1 = rh1.get(r1 - c, r1), h2 = rh2.get(r2 - c, r2);
(h1 == h2 ? lo : hi) = c;
}
return lo;
}
#line 3 "string/test/rolling_hash_mersenne61.test.cpp"
#include <iostream>
#line 5 "string/test/rolling_hash_mersenne61.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_14_B"
using namespace std;
int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);
string T, P;
cin >> T >> P;
rolling_hash<ModIntMersenne61> rh_T(T), rh_P(P);
for (int l = 0; l < (int)(T.length() - P.length() + 1); l++) {
if (rh_T.get(l, l + P.length()) == rh_P.get(0, P.length())) cout << l << '\n';
}
}