Algorithm (Rolling Hash)
- Brute force compare the hash of all substrings.
- The hash could be computed efficiently using rolling hash technique.
- Total complexity is $O(n^{2})$ where $n$ is the length of the input string.
Code
namespace math::field {
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Z {
public:
using value_type = typename T::value_type;
constexpr static value_type mod() { return T::value; }
// constructors
constexpr Z() : value() {}
template <typename U> Z(const U& x) { value = normalize(x); }
template <typename U> static value_type normalize(const U& x) {
if (0 <= x and x < mod()) return value_type(x);
else if (x >= mod()) return value_type(x % mod());
else return value_type(x + mod());
}
Z& operator+=(const Z& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
Z& operator-=(const Z& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
Z& operator++() { return *this += 1; }
Z& operator--() { return *this -= 1; }
Z operator++(int) { Z result(*this); *this += 1; return result; }
Z operator--(int) { Z result(*this); *this -= 1; return result; }
Z operator-() const { return Z(-value); }
Z& operator*=(const Z& rhs) { value = (value % mod() * rhs.value % mod()) % mod(); return *this; }
Z& operator/=(const Z& other) { return *this *= Z(inverse(other.value, mod())); }
const value_type& operator()() const { return value; }
template <typename U> explicit operator U() const { return static_cast<U>(value); }
template <typename U> Z& operator+=(const U& other) { return *this += Z(other); }
template <typename U> Z& operator-=(const U& other) { return *this -= Z(other); }
template <typename U> friend const Z<U>& abs(const Z<U>& v) { return v; }
template <typename U> friend bool operator==(const Z<U>& lhs, const Z<U>& rhs);
template <typename U> friend bool operator<(const Z<U>& lhs, const Z<U>& rhs);
template <typename U> friend std::istream& operator>>(std::istream& stream, Z<U>& number);
private:
value_type value;
};
template <typename T> bool operator==(const Z<T>& lhs, const Z<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Z<T>& lhs, U rhs) { return lhs == Z<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Z<T>& rhs) { return Z<T>(lhs) == rhs; }
template <typename T> bool operator!=(const Z<T>& lhs, const Z<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Z<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Z<T>& rhs) { return !(lhs == rhs); }
template <typename T> bool operator<(const Z<T>& lhs, const Z<T>& rhs) { return lhs.value < rhs.value; }
template <typename T> Z<T> operator+(const Z<T>& lhs, const Z<T>& rhs) { return Z<T>(lhs) += rhs; }
template <typename T, typename U> Z<T> operator+(const Z<T>& lhs, U rhs) { return Z<T>(lhs) += rhs; }
template <typename T, typename U> Z<T> operator+(U lhs, const Z<T>& rhs) { return Z<T>(lhs) += rhs; }
template <typename T> Z<T> operator-(const Z<T>& lhs, const Z<T>& rhs) { return Z<T>(lhs) -= rhs; }
template <typename T, typename U> Z<T> operator-(const Z<T>& lhs, U rhs) { return Z<T>(lhs) -= rhs; }
template <typename T, typename U> Z<T> operator-(U lhs, const Z<T>& rhs) { return Z<T>(lhs) -= rhs; }
template <typename T> Z<T> operator*(const Z<T>& lhs, const Z<T>& rhs) { return Z<T>(lhs) *= rhs; }
template <typename T, typename U> Z<T> operator*(const Z<T>& lhs, U rhs) { return Z<T>(lhs) *= rhs; }
template <typename T, typename U> Z<T> operator*(U lhs, const Z<T>& rhs) { return Z<T>(lhs) *= rhs; }
template <typename T> Z<T> operator/(const Z<T>& lhs, const Z<T>& rhs) { return Z<T>(lhs) /= rhs; }
template <typename T, typename U> Z<T> operator/(const Z<T>& lhs, U rhs) { return Z<T>(lhs) /= rhs; }
template <typename T, typename U> Z<T> operator/(U lhs, const Z<T>& rhs) { return Z<T>(lhs) /= rhs; }
// using namespace std;;
template<typename T, typename U>
Z<T> power(const Z<T>& a, const U& b) {
assert(b >= 0);
Z<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
bool is_zero(const Z<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Z<T>& number) {
return to_string(number());
}
template <typename T>
std::ostream& operator<<(std::ostream& stream, const Z<T>& number) {
return stream << number();
}
template <typename T>
std::istream& operator>>(std::istream& stream, Z<T>& number) {
typename common_type<typename Z<T>::value_type, long long>::type x;
stream >> x;
number.value = Z<T>::normalize(x);
return stream;
}
} // end of namespace
template<typename T> struct std::hash<math::field::Z<T>> {
std::size_t operator()(math::field::Z<T> const& z) const noexcept {
return std::hash<typename T::value_type>{}(z());
}
};
template <class F>
struct recursive {
F f;
template <class... Ts>
decltype(auto) operator()(Ts&&... ts) const { return f(std::ref(*this), std::forward<Ts>(ts)...); }
template <class... Ts>
decltype(auto) operator()(Ts&&... ts) { return f(std::ref(*this), std::forward<Ts>(ts)...); }
};
template <class F> recursive(F) -> recursive<F>;
auto const rec = [](auto f){ return recursive{std::move(f)}; };
class Solution {
public:
int distinctEchoSubstrings(string text) {
typedef std::integral_constant<long long, (long long)(1e9+7)> mod;
using math::field::Z;
const auto N = size(text);
static const auto power = [&](vector<Z<mod>> self = {}) {
self.resize(2000, 1);
for (int i = 1; i < 2000; ++i)
self[i] = self[i - 1] * 256;
return self;
}();
auto ascii = [](char ch) { return int(ch); };
const auto prefix_rolling_hash = [&](vector<Z<mod>> self = {}) {
self.resize(N);
auto go = rec([&](auto&&go, int i) -> void {
if (i == size(text))
return;
else if (i == 0)
(self[i] = ascii(text[i]), go(i + 1));
else
(self[i] = 256 * self[i - 1] + ascii(text[i]), go(i + 1));
});
return (go(0), self);
}();
auto substr_rolling_hash = [&](int i, int j) {
if (i == 0)
return prefix_rolling_hash[j];
else
return prefix_rolling_hash[j] - prefix_rolling_hash[i - 1] * power[j - i + 1];
};
const auto solution = [&](unordered_set<Z<mod>> acc = {}) {
auto go = rec([&](auto&&go, int i, int len) -> void {
if (i + 2 * len - 1 >= N)
return;
else if (substr_rolling_hash(i, i + len - 1) == substr_rolling_hash(i + len, i + 2 *len - 1))
(acc.emplace(substr_rolling_hash(i, i + len - 1)), go(i + 1, len));
else
go(i + 1, len);
});
for (int len = 1; len <= N / 2; ++len)
go(0, len);
return size(acc);
}();
return solution;
}
};