結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー hitonanodehitonanode
提出日時 2022-10-14 21:48:39
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,367 ms / 3,000 ms
コード長 15,171 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 188,332 KB
実行使用メモリ 112,876 KB
最終ジャッジ日時 2024-06-26 13:39:34
合計ジャッジ時間 60,580 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
20,364 KB
testcase_01 AC 17 ms
20,516 KB
testcase_02 AC 435 ms
43,136 KB
testcase_03 AC 647 ms
55,388 KB
testcase_04 AC 468 ms
42,004 KB
testcase_05 AC 213 ms
29,512 KB
testcase_06 AC 476 ms
43,520 KB
testcase_07 AC 997 ms
74,624 KB
testcase_08 AC 51 ms
21,248 KB
testcase_09 AC 698 ms
61,952 KB
testcase_10 AC 134 ms
26,572 KB
testcase_11 AC 191 ms
34,640 KB
testcase_12 AC 380 ms
41,856 KB
testcase_13 AC 834 ms
67,584 KB
testcase_14 AC 315 ms
35,840 KB
testcase_15 AC 535 ms
44,584 KB
testcase_16 AC 994 ms
65,408 KB
testcase_17 AC 579 ms
55,680 KB
testcase_18 AC 960 ms
73,728 KB
testcase_19 AC 223 ms
32,528 KB
testcase_20 AC 1,024 ms
70,784 KB
testcase_21 AC 88 ms
21,892 KB
testcase_22 AC 1,056 ms
73,344 KB
testcase_23 AC 1,166 ms
73,228 KB
testcase_24 AC 1,116 ms
73,344 KB
testcase_25 AC 1,158 ms
73,344 KB
testcase_26 AC 1,046 ms
73,216 KB
testcase_27 AC 1,056 ms
73,344 KB
testcase_28 AC 1,063 ms
73,216 KB
testcase_29 AC 1,059 ms
73,272 KB
testcase_30 AC 1,054 ms
73,216 KB
testcase_31 AC 1,062 ms
73,344 KB
testcase_32 AC 1,062 ms
73,216 KB
testcase_33 AC 1,081 ms
73,344 KB
testcase_34 AC 1,188 ms
73,216 KB
testcase_35 AC 1,150 ms
73,256 KB
testcase_36 AC 1,083 ms
73,216 KB
testcase_37 AC 1,145 ms
73,344 KB
testcase_38 AC 1,090 ms
73,344 KB
testcase_39 AC 1,170 ms
73,344 KB
testcase_40 AC 1,099 ms
73,296 KB
testcase_41 AC 1,099 ms
73,228 KB
testcase_42 AC 752 ms
38,728 KB
testcase_43 AC 756 ms
38,784 KB
testcase_44 AC 758 ms
38,784 KB
testcase_45 AC 729 ms
38,668 KB
testcase_46 AC 734 ms
38,732 KB
testcase_47 AC 1,158 ms
83,072 KB
testcase_48 AC 1,153 ms
82,924 KB
testcase_49 AC 1,157 ms
83,044 KB
testcase_50 AC 1,163 ms
82,924 KB
testcase_51 AC 1,160 ms
83,072 KB
testcase_52 AC 1,343 ms
112,720 KB
testcase_53 AC 1,367 ms
112,876 KB
testcase_54 AC 1,363 ms
112,748 KB
testcase_55 AC 1,219 ms
80,688 KB
testcase_56 AC 1,244 ms
80,648 KB
testcase_57 AC 1,226 ms
80,764 KB
testcase_58 AC 97 ms
37,376 KB
testcase_59 AC 1,237 ms
78,720 KB
testcase_60 AC 46 ms
20,580 KB
testcase_61 AC 1,203 ms
80,672 KB
testcase_62 AC 26 ms
20,736 KB
testcase_63 AC 191 ms
20,616 KB
testcase_64 AC 168 ms
20,608 KB
testcase_65 AC 223 ms
20,992 KB
testcase_66 AC 1,223 ms
80,256 KB
testcase_67 AC 100 ms
20,536 KB
testcase_68 AC 87 ms
20,692 KB
testcase_69 AC 96 ms
20,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T, typename V>
void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }
template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <class T1, class T2> std::pair<T1, T2> operator+(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) { return std::make_pair(l.first + r.first, l.second + r.second); }
template <class T1, class T2> std::pair<T1, T2> operator-(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) { return std::make_pair(l.first - r.first, l.second - r.second); }
template <class T> std::vector<T> sort_unique(std::vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <class T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }
template <class T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }
template <class IStream, class T> IStream &operator>>(IStream &is, std::vector<T> &vec) { for (auto &v : vec) is >> v; return is; }

template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);

template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
#endif
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl
#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr)
#else
#define dbg(x) ((void)0)
#define dbgif(cond, x) ((void)0)
#endif

#include <algorithm>
#include <chrono>
#include <random>
#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 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;
    }
};

// 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();
    }
    V get(int l, int r) const { // s[l] * B^(r - l - 1) + ... + s[r - 1]
        return 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;
}

#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) {}

    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;
    }
};


int main() {
    int N;
    cin >> N;
    using Hash = PairHash<ModIntMersenne61, ModIntMersenne61>;
    set<Hash> se;

    vector<Hash> bpow(1.1e6, Hash(1));
    FOR(i, 1, bpow.size()) bpow.at(i) = bpow.at(i - 1) * Hash::randgen();
    dbg(bpow[0]);
    dbg(bpow[1]);
    dbg(bpow[2]);
    dbg(bpow[1] * bpow[1]);

    while (N--) {
        string s;
        cin >> s;
        rolling_hash<Hash> rh(s);
        cout << (se.count(rh.get(0, s.size())) ? "Yes" : "No") << '\n';

        se.insert(rh.get(0, s.size()));
        dbg(rh.get(0, s.size()));
        FOR(i, 1, s.size()) {
            auto left = rh.get(0, i - 1);
            auto right = rh.get(i + 1, s.size());
            auto tmp = left * bpow[1] + s[i];
            tmp = tmp * bpow[1] + s[i - 1];
            tmp = tmp * bpow[s.size() - i - 1] + right;
            dbg(tmp);
            se.insert(tmp);
        }
        dbg(s);
        dbg(se.size());
    }
}
0