This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/14/ALDS1_14_B"
#include "../container/polynomial_hash.cpp"
#include <iostream>
#include "../utility/rep.cpp"
int main() {
std::string T, P;
std::cin >> T >> P;
if (T.size() < P.size()) {
return 0;
}
PolynomialHash rh(std::vector<char>(T.begin(), T.end()));
const auto match = PolynomialHash(std::vector<char>(P.begin(), P.end())).fold();
for (const int i : rep(0, T.size() - P.size() + 1)) {
if (rh.fold(i, i + P.size()) == match) {
std::cout << i << '\n';
}
}
return 0;
}
#line 1 "test/polynomial_hash.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/14/ALDS1_14_B"
#line 2 "container/polynomial_hash.cpp"
#include <cassert>
#include <string>
#include <type_traits>
#include <vector>
#line 2 "random/rand_int.cpp"
#include <random>
#line 2 "random/xorshift.cpp"
#include <chrono>
#line 2 "utility/int_alias.cpp"
#include <cstdint>
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 5 "random/xorshift.cpp"
u64 xorshift() {
static u64 state = std::chrono::system_clock::now().time_since_epoch().count();
state ^= state << 7;
state ^= state >> 9;
return state;
}
#line 4 "random/rand_int.cpp"
template <class T> T rand_int(const T& min, const T& max) {
static std::default_random_engine gen(xorshift());
return std::uniform_int_distribution<T>(min, max)(gen);
}
#line 2 "utility/rep.cpp"
#include <algorithm>
class Range {
struct Iter {
int itr;
constexpr Iter(const int pos) noexcept : itr(pos) {}
constexpr void operator++() noexcept { ++itr; }
constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
constexpr int operator*() const noexcept { return itr; }
};
const Iter first, last;
public:
explicit constexpr Range(const int first, const int last) noexcept : first(first), last(std::max(first, last)) {}
constexpr Iter begin() const noexcept { return first; }
constexpr Iter end() const noexcept { return last; }
};
constexpr Range rep(const int l, const int r) noexcept { return Range(l, r); }
constexpr Range rep(const int n) noexcept { return Range(0, n); }
#line 9 "container/polynomial_hash.cpp"
struct PolynomialHashOperations {
static inline constexpr u64 MOD = ((u64)1 << 61) - 1;
static constexpr u64 sum(u64 a, const u64 b) { return (a += b) >= MOD ? a - MOD : a; }
static constexpr u64 difference(u64 a, const u64 b) { return (a += MOD - b) >= MOD ? a - MOD : a; }
static constexpr u64 product(const u64 a, const u64 b) {
u128 ret = (u128)a * b;
ret = (ret >> 61) + (ret & MOD);
return ret >= MOD ? ret - MOD : ret;
}
};
template <int ID> struct PolynomialHashBase {
static inline const u64 BASE = rand_int<u64>(0, PolynomialHashOperations::MOD - 1);
static u64 base_pow(const int n) {
static std::vector<u64> vec;
if (vec.empty()) vec = {1};
while ((int)vec.size() <= n) vec.push_back(PolynomialHashOperations::product(vec.back(), BASE));
return vec[n];
}
};
template <class T, int ID = -1, std::enable_if_t<std::is_integral_v<T>>* = nullptr> class PolynomialHash {
using Oper = PolynomialHashOperations;
using Base = PolynomialHashBase<ID>;
std::vector<u64> hash;
std::vector<T> data;
public:
PolynomialHash() : PolynomialHash(std::vector<T>()) {}
explicit PolynomialHash(const std::vector<T>& vec) : data(vec) {
const int size = data.size();
hash = std::vector<u64>(size + 1);
for (const int i : rep(size)) {
assert(0 <= data[i] and (u64) data[i] < Oper::MOD);
hash[i + 1] = Oper::sum(Oper::product(Base::BASE, hash[i]), (u64)data[i]);
}
}
int size() const { return data.size(); }
const std::vector<T>& get() const { return data; }
u64 fold() const { return hash.back(); }
u64 fold(const int l, const int r) const {
assert(0 <= l and l <= r and r <= size());
return Oper::difference(hash[r], Oper::product(hash[l], Base::base_pow(r - l)));
}
void concat(const PolynomialHash& other) {
hash.reserve(hash.size() + other.size());
u64 val = hash.back();
for (const int i : rep(other.size())) {
val = Oper::product(val, Base::BASE);
hash.push_back(Oper::sum(val, other.hash[i + 1]));
}
data.reserve(data.size() + other.size());
std::copy(other.data.cbegin(), other.data.cend(), std::back_inserter(data));
}
};
#line 3 "test/polynomial_hash.test.cpp"
#include <iostream>
#line 5 "test/polynomial_hash.test.cpp"
int main() {
std::string T, P;
std::cin >> T >> P;
if (T.size() < P.size()) {
return 0;
}
PolynomialHash rh(std::vector<char>(T.begin(), T.end()));
const auto match = PolynomialHash(std::vector<char>(P.begin(), P.end())).fold();
for (const int i : rep(0, T.size() - P.size() + 1)) {
if (rh.fold(i, i + P.size()) == match) {
std::cout << i << '\n';
}
}
return 0;
}