This documentation is automatically generated by online-judge-tools/verification-helper
#include "../../modint.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;
using mint1 = ModInt<998244353>;
using mint2 = ModInt<1000000007>;
int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);
mint1 b1 = 51152368;
mint2 b2 = 1537689;
string T, P;
cin >> T >> P;
rolling_hash<mint1> rh_T1(T, b1), rh_P1(P, b1);
rolling_hash<mint2> rh_T2(T, b2), rh_P2(P, b2);
for (int l = 0; l < (int)(T.length() - P.length() + 1); l++) {
if (rh_T1.get(l, l + P.length()) == rh_P1.get(0, P.length()) and
rh_T2.get(l, l + P.length()) == rh_P2.get(0, P.length())) {
cout << l << '\n';
}
}
}
#line 2 "modint.hpp"
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
template <int md> struct ModInt {
using lint = long long;
constexpr static int mod() { return md; }
static int get_primitive_root() {
static int primitive_root = 0;
if (!primitive_root) {
primitive_root = [&]() {
std::set<int> fac;
int v = md - 1;
for (lint i = 2; i * i <= v; i++)
while (v % i == 0) fac.insert(i), v /= i;
if (v > 1) fac.insert(v);
for (int g = 1; g < md; g++) {
bool ok = true;
for (auto i : fac)
if (ModInt(g).pow((md - 1) / i) == 1) {
ok = false;
break;
}
if (ok) return g;
}
return -1;
}();
}
return primitive_root;
}
int val_;
int val() const noexcept { return val_; }
constexpr ModInt() : val_(0) {}
constexpr ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; }
constexpr ModInt(lint v) { _setval(v % md + md); }
constexpr explicit operator bool() const { return val_ != 0; }
constexpr ModInt operator+(const ModInt &x) const {
return ModInt()._setval((lint)val_ + x.val_);
}
constexpr ModInt operator-(const ModInt &x) const {
return ModInt()._setval((lint)val_ - x.val_ + md);
}
constexpr ModInt operator*(const ModInt &x) const {
return ModInt()._setval((lint)val_ * x.val_ % md);
}
constexpr ModInt operator/(const ModInt &x) const {
return ModInt()._setval((lint)val_ * x.inv().val() % md);
}
constexpr ModInt operator-() const { return ModInt()._setval(md - val_); }
constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt(a) + x; }
friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt(a) - x; }
friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt(a) * x; }
friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt(a) / x; }
constexpr bool operator==(const ModInt &x) const { return val_ == x.val_; }
constexpr bool operator!=(const ModInt &x) const { return val_ != x.val_; }
constexpr bool operator<(const ModInt &x) const {
return val_ < x.val_;
} // To use std::map<ModInt, T>
friend std::istream &operator>>(std::istream &is, ModInt &x) {
lint t;
return is >> t, x = ModInt(t), is;
}
constexpr friend std::ostream &operator<<(std::ostream &os, const ModInt &x) {
return os << x.val_;
}
constexpr ModInt pow(lint n) const {
ModInt ans = 1, tmp = *this;
while (n) {
if (n & 1) ans *= tmp;
tmp *= tmp, n >>= 1;
}
return ans;
}
static constexpr int cache_limit = std::min(md, 1 << 21);
static std::vector<ModInt> facs, facinvs, invs;
constexpr static void _precalculation(int N) {
const int l0 = facs.size();
if (N > md) N = md;
if (N <= l0) return;
facs.resize(N), facinvs.resize(N), invs.resize(N);
for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i;
facinvs[N - 1] = facs.back().pow(md - 2);
for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1);
for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1];
}
constexpr ModInt inv() const {
if (this->val_ < cache_limit) {
if (facs.empty()) facs = {1}, facinvs = {1}, invs = {0};
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return invs[this->val_];
} else {
return this->pow(md - 2);
}
}
constexpr ModInt fac() const {
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return facs[this->val_];
}
constexpr ModInt facinv() const {
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return facinvs[this->val_];
}
constexpr ModInt doublefac() const {
lint k = (this->val_ + 1) / 2;
return (this->val_ & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac())
: ModInt(k).fac() * ModInt(2).pow(k);
}
constexpr ModInt nCr(int r) const {
if (r < 0 or this->val_ < r) return ModInt(0);
return this->fac() * (*this - r).facinv() * ModInt(r).facinv();
}
constexpr ModInt nPr(int r) const {
if (r < 0 or this->val_ < r) return ModInt(0);
return this->fac() * (*this - r).facinv();
}
static ModInt binom(int n, int r) {
static long long bruteforce_times = 0;
if (r < 0 or n < r) return ModInt(0);
if (n <= bruteforce_times or n < (int)facs.size()) return ModInt(n).nCr(r);
r = std::min(r, n - r);
ModInt ret = ModInt(r).facinv();
for (int i = 0; i < r; ++i) ret *= n - i;
bruteforce_times += r;
return ret;
}
// Multinomial coefficient, (k_1 + k_2 + ... + k_m)! / (k_1! k_2! ... k_m!)
// Complexity: O(sum(ks))
template <class Vec> static ModInt multinomial(const Vec &ks) {
ModInt ret{1};
int sum = 0;
for (int k : ks) {
assert(k >= 0);
ret *= ModInt(k).facinv(), sum += k;
}
return ret * ModInt(sum).fac();
}
// Catalan number, C_n = binom(2n, n) / (n + 1)
// C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, ...
// https://oeis.org/A000108
// Complexity: O(n)
static ModInt catalan(int n) {
if (n < 0) return ModInt(0);
return ModInt(n * 2).fac() * ModInt(n + 1).facinv() * ModInt(n).facinv();
}
ModInt sqrt() const {
if (val_ == 0) return 0;
if (md == 2) return val_;
if (pow((md - 1) / 2) != 1) return 0;
ModInt b = 1;
while (b.pow((md - 1) / 2) == 1) b += 1;
int e = 0, m = md - 1;
while (m % 2 == 0) m >>= 1, e++;
ModInt x = pow((m - 1) / 2), y = (*this) * x * x;
x *= (*this);
ModInt z = b.pow(m);
while (y != 1) {
int j = 0;
ModInt t = y;
while (t != 1) j++, t *= t;
z = z.pow(1LL << (e - j - 1));
x *= z, z *= z, y *= z;
e = j;
}
return ModInt(std::min(x.val_, md - x.val_));
}
};
template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0};
using ModInt998244353 = ModInt<998244353>;
// using mint = ModInt<998244353>;
// using mint = ModInt<1000000007>;
#line 2 "string/rolling_hash_1d.hpp"
#include <algorithm>
#include <chrono>
#include <random>
#include <string>
#include <tuple>
#line 8 "string/rolling_hash_1d.hpp"
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 5 "string/test/rolling_hash_w_modint.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_14_B"
using namespace std;
using mint1 = ModInt<998244353>;
using mint2 = ModInt<1000000007>;
int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);
mint1 b1 = 51152368;
mint2 b2 = 1537689;
string T, P;
cin >> T >> P;
rolling_hash<mint1> rh_T1(T, b1), rh_P1(P, b1);
rolling_hash<mint2> rh_T2(T, b2), rh_P2(P, b2);
for (int l = 0; l < (int)(T.length() - P.length() + 1); l++) {
if (rh_T1.get(l, l + P.length()) == rh_P1.get(0, P.length()) and
rh_T2.get(l, l + P.length()) == rh_P2.get(0, P.length())) {
cout << l << '\n';
}
}
}