結果

問題 No.1695 Mirror Mirror
ユーザー packer_jppacker_jp
提出日時 2021-10-01 22:47:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 12,068 bytes
コンパイル時間 2,808 ms
コンパイル使用メモリ 218,220 KB
実行使用メモリ 51,916 KB
最終ジャッジ日時 2023-08-28 16:07:23
合計ジャッジ時間 13,941 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 246 ms
51,916 KB
testcase_22 AC 298 ms
51,432 KB
testcase_23 AC 317 ms
50,480 KB
testcase_24 AC 310 ms
50,820 KB
testcase_25 AC 194 ms
33,936 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 249 ms
41,476 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 199 ms
34,780 KB
testcase_31 AC 280 ms
47,284 KB
testcase_32 AC 68 ms
13,968 KB
testcase_33 AC 299 ms
49,744 KB
testcase_34 AC 126 ms
23,144 KB
testcase_35 AC 254 ms
42,528 KB
testcase_36 AC 318 ms
50,756 KB
testcase_37 AC 111 ms
20,380 KB
testcase_38 AC 254 ms
43,144 KB
testcase_39 AC 96 ms
17,964 KB
testcase_40 AC 282 ms
47,292 KB
testcase_41 AC 311 ms
51,284 KB
testcase_42 AC 315 ms
51,020 KB
testcase_43 AC 198 ms
33,052 KB
testcase_44 AC 238 ms
40,020 KB
testcase_45 AC 191 ms
32,404 KB
testcase_46 AC 221 ms
37,984 KB
testcase_47 AC 216 ms
36,240 KB
testcase_48 AC 305 ms
48,904 KB
testcase_49 AC 314 ms
51,228 KB
testcase_50 AC 291 ms
49,396 KB
testcase_51 AC 312 ms
51,268 KB
testcase_52 AC 197 ms
34,896 KB
testcase_53 AC 191 ms
33,836 KB
testcase_54 AC 190 ms
32,324 KB
testcase_55 AC 182 ms
31,800 KB
testcase_56 AC 225 ms
39,196 KB
testcase_57 AC 188 ms
33,676 KB
testcase_58 AC 257 ms
44,328 KB
testcase_59 AC 221 ms
38,908 KB
testcase_60 AC 192 ms
33,468 KB
testcase_61 AC 110 ms
21,496 KB
testcase_62 AC 303 ms
48,780 KB
testcase_63 AC 301 ms
50,020 KB
testcase_64 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define uniq(a) (a).erase(unique(all(a)), (a).end())
#define SZ(x) ((int)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define vsum(x) reduce(all(x))
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define mp make_pair
#define endl '\n'
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vc = vector<char>;
using VVi = vector<vector<int>>;
using VVl = vector<vector<ll>>;
using VVc = vector<vector<char>>;
template <typename T, typename U> using P = pair<T, U>;
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
constexpr ll inf = 1000000000ll;
constexpr ll INF = 4000000004000000000LL;
constexpr ld eps = 1e-15;
constexpr ld PI = 3.141592653589793;
constexpr int popcnt(ull x) { return __builtin_popcountll(x); }
template <typename T> using mat = vector<vector<T>>;
constexpr ll dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr ll dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr ll sign(ll a) { return (a > 0) - (a < 0); }
constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
constexpr ll cdiv(ll a, ll b) { return -fdiv(-a, b); }
constexpr ull bit(int n) { return 1ull << n; }
template <typename T> constexpr T mypow(T x, ll n) {
    T ret = 1;
    while (n) {
        if (n & 1) ret *= x;
        x *= x;
        n >>= 1;
    }
    return ret;
}
constexpr ll modpow(ll x, ll n, ll mod) {
    ll ret = 1;
    while (n) {
        if (n & 1) ret *= x;
        x *= x;
        n >>= 1;
        x %= mod;
        ret %= mod;
    }
    return ret;
}
template <typename T> T xor64(T lb, T ub) {
    static ull x = 88172645463325252ull;
    x ^= x << 7;
    return lb + (x ^= x >> 9) % (ub - lb);
}
constexpr ll safemod(ll x, ll mod) { return (x % mod + mod) % mod; }
template <typename T> constexpr T sq(const T &a) { return a * a; }
template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename U> bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <typename T, typename U> bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <typename T> T make_vector(T &&a) { return a; }
template <typename... Ts> auto make_vector(int h, Ts &&... ts) { return vector(h, make_vector(ts...)); }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) {
    os << "(" << a.first << ", " << a.second << ")";
    return os;
}
template <typename T, typename U, typename V> ostream &operator<<(ostream &os, const tuple<T, U, V> &a) {
    os << "(" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> void print(const T &a) { cout << a << endl; }
template <typename T> void print(const vector<T> &v) {
    for (auto &e : v) cout << e << " ";
    cout << endl;
}
template <typename T> void scan(vector<T> &a) {
    for (auto &i : a) cin >> i;
}
struct timer {
    clock_t start_time;
    void start() { start_time = clock(); }
    int lap() {
        // return x ms.
        return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
    }
};
template <typename T = int> struct Edge {
    int from, to;
    T cost;
    int idx;

    Edge() = default;

    Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}

    operator int() const { return to; }
};
template <typename T = int> struct Graph {
    vector<vector<Edge<T>>> g;
    int es;

    Graph() = default;

    explicit Graph(int n) : g(n), es(0) {}

    size_t size() const { return g.size(); }

    void add_directed_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es++); }

    void add_edge(int from, int to, T cost = 1) {
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }

    void read(int M, int padding = -1, bool weighted = false, bool directed = false) {
        for (int i = 0; i < M; i++) {
            int a, b;
            cin >> a >> b;
            a += padding;
            b += padding;
            T c = T(1);
            if (weighted) cin >> c;
            if (directed)
                add_directed_edge(a, b, c);
            else
                add_edge(a, b, c);
        }
    }
};
#ifdef ONLINE_JUDGE
#define dump(...) (void(0))
#else
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head &&head, Tail &&... tail) {
    cerr << head;
    if (sizeof...(Tail)) cerr << ", ";
    debug(tail...);
}
#define dump(...) cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ", debug(__VA_ARGS__)
#endif
struct rep {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { ++v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v < *i; }
    };
    ll l, r;
    rep(ll l, ll r) : l(l), r(r) {}
    rep(ll r) : rep(0, r) {}
    itr begin() const { return l; };
    itr end() const { return r; };
};
struct per {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { --v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v > *i; }
    };
    ll l, r;
    per(ll l, ll r) : l(l), r(r) {}
    per(ll r) : per(0, r) {}
    itr begin() const { return r - 1; };
    itr end() const { return l - 1; };
};
struct io_setup {
    static constexpr int PREC = 20;
    io_setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} iOS;
template <ll MOD = 1000000007> struct modint {
    ll val;
    modint(ll val = 0) : val(val >= 0 ? val % MOD : (MOD - (-val) % MOD) % MOD) {}
    static ll mod() { return MOD; }
    modint inv() const {
        ll a = val, b = MOD, u = 1, v = 0, t;
        while (b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return modint(u);
    }
    modint pow(ll k) const {
        modint ret = 1, mul = val;
        while (k) {
            if (k & 1) ret *= mul;
            mul *= mul;
            k >>= 1;
        }
        return ret;
    }
    modint &operator+=(const modint &a) {
        if ((val += a.val) >= MOD) val -= MOD;
        return *this;
    }
    modint &operator-=(const modint &a) {
        if ((val += MOD - a.val) >= MOD) val -= MOD;
        return *this;
    }
    modint &operator*=(const modint &a) {
        (val *= a.val) %= MOD;
        return *this;
    }
    modint &operator/=(const modint &a) { return *this *= a.inv(); }
    modint operator+() const { return *this; }
    modint operator-() const { return modint(-val); }
    friend bool operator==(const modint &a, const modint &b) { return a.val == b.val; }
    friend bool operator!=(const modint &a, const modint &b) { return rel_ops::operator!=(a, b); }
    friend modint operator+(const modint &a, const modint &b) { return modint(a) += b; }
    friend modint operator-(const modint &a, const modint &b) { return modint(a) -= b; }
    friend modint operator*(const modint &a, const modint &b) { return modint(a) *= b; }
    friend modint operator/(const modint &a, const modint &b) { return modint(a) /= b; }
    friend istream &operator>>(istream &is, modint &a) {
        ll val;
        is >> val;
        a = modint(val);
        return is;
    }
    friend ostream &operator<<(ostream &os, const modint &a) { return os << a.val; }
};

struct rolling_hash {
    static constexpr ull MOD = bit(61) - 1;
    static vector<ull> pbase;
    vector<ull> hash;
    static void resize_pbase(int n) {
        int sz = pbase.size();
        if (sz > n) return;
        pbase.resize(n + 1);
        for (int i : rep(sz - 1, n)) pbase[i + 1] = mul(pbase[i], pbase[1]);
    }
    template <typename T> static T calc_mod(T val) {
        val = (val & MOD) + (val >> 61);
        if (val >= MOD) val -= MOD;
        return val;
    }
    static ull mul(ull a, ull b) { return calc_mod((__uint128_t)a * b); }
    static ull concat(ull lhs, ull rhs, int rn) {
        resize_pbase(rn);
        return calc_mod(mul(lhs, pbase[rn]) + rhs);
    }
    rolling_hash(const string &src) : hash(src.size() + 1) {
        for (int i : rep(src.size())) hash[i + 1] = calc_mod(mul(hash[i], pbase[1]) + src[i]);
        resize_pbase(src.size());
    }
    template <typename T> rolling_hash(const vector<T> &src) : hash(src.size() + 1) {
        for (int i : rep(src.size())) hash[i + 1] = calc_mod(mul(hash[i], pbase[1]) + src[i]);
        resize_pbase(src.size());
    }
    ull get_hash(int l, int r) const { return calc_mod(MOD - mul(hash[l], pbase[r - l]) + hash[r]); }
};
vector<ull> rolling_hash::pbase{1, xor64(MOD >> 1, MOD)};

template <typename F> ll bisect(ll ok, ll ng, F f) {
    while (abs(ok - ng) > 1) {
        ll mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

struct oibfs {
    struct edge {
        int to, cost;
    };
    vector<vector<edge>> g;
    oibfs(int n) : g(n) {}
    void add_edge(int from, int to, int cost) { g[from].push_back({to, cost}); }
    pair<vector<int>, vector<int>> get(int s) const {
        vector<int> dist(g.size(), INT_MAX), prev(g.size(), -1);
        using P = pair<int, int>;
        deque<P> deq;
        dist[s] = 0;
        deq.emplace_front(0, s);
        while (!deq.empty()) {
            auto [d, from] = deq.front();
            deq.pop_front();
            if (dist[from] < d) continue;
            for (auto [to, cost] : g[from]) {
                int nd = dist[from] + cost;
                if (nd < dist[to]) {
                    dist[to] = nd;
                    prev[to] = from;
                    if (cost == 0) deq.emplace_front(nd, to);
                    if (cost == 1) deq.emplace_back(nd, to);
                }
            }
        }
        return {dist, prev};
    }
};

int main() {
    ll n, m;
    cin >> n >> m;
    string s, t;
    cin >> s >> t;
    rolling_hash ord(t);
    reverse(all(t));
    rolling_hash rev(t);
    reverse(all(t));
    oibfs bfs(t.size() + 1);
    for (ll i : rep(1, t.size())) {
        ll m = bisect(0, t.size(), [&](ll x) -> bool {
            return i >= x && t.size() - i >= x &&
                   ord.get_hash(i - x, i) == rev.get_hash(t.size() - i - x, t.size() - i);
        });
        bfs.add_edge(i, i + m, 1);
    }
    for (ll i : rep(t.size())) bfs.add_edge(i + 1, i, 0);
    auto f = [&](string s) -> ll {
        ll from;
        for (from = 0; from < min(s.size(), t.size()); ++from) {
            if (s[from] != t[from]) break;
        }
        return from;
    };
    ll from = f(s);
    reverse(all(s));
    chmax(from, f(s));
    ll ans = bfs.get(from).first[t.size()];
    if (ans == INT_MAX)
        cout << -1 << endl;
    else
        cout << ans << endl;
}
0