結果

問題 No.1695 Mirror Mirror
ユーザー KudeKude
提出日時 2021-10-01 23:14:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 2,820 bytes
コンパイル時間 4,362 ms
コンパイル使用メモリ 265,148 KB
実行使用メモリ 100,188 KB
最終ジャッジ日時 2023-08-28 16:09:02
合計ジャッジ時間 11,691 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 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 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 117 ms
96,456 KB
testcase_22 AC 113 ms
96,600 KB
testcase_23 AC 170 ms
100,188 KB
testcase_24 AC 127 ms
96,000 KB
testcase_25 AC 145 ms
63,296 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 134 ms
78,144 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 108 ms
65,320 KB
testcase_31 AC 129 ms
90,052 KB
testcase_32 AC 33 ms
22,464 KB
testcase_33 AC 175 ms
95,808 KB
testcase_34 AC 74 ms
40,820 KB
testcase_35 AC 148 ms
80,476 KB
testcase_36 AC 178 ms
98,692 KB
testcase_37 AC 52 ms
35,548 KB
testcase_38 AC 114 ms
81,452 KB
testcase_39 AC 47 ms
30,888 KB
testcase_40 AC 122 ms
89,316 KB
testcase_41 AC 123 ms
97,576 KB
testcase_42 AC 165 ms
99,100 KB
testcase_43 AC 105 ms
62,424 KB
testcase_44 AC 125 ms
77,292 KB
testcase_45 AC 111 ms
60,796 KB
testcase_46 AC 101 ms
71,424 KB
testcase_47 AC 101 ms
67,620 KB
testcase_48 AC 145 ms
93,132 KB
testcase_49 AC 130 ms
98,604 KB
testcase_50 AC 176 ms
96,640 KB
testcase_51 AC 172 ms
98,360 KB
testcase_52 AC 122 ms
66,872 KB
testcase_53 AC 113 ms
65,564 KB
testcase_54 AC 107 ms
60,540 KB
testcase_55 AC 97 ms
60,228 KB
testcase_56 AC 128 ms
74,936 KB
testcase_57 AC 105 ms
63,496 KB
testcase_58 AC 151 ms
85,260 KB
testcase_59 AC 121 ms
73,944 KB
testcase_60 AC 106 ms
62,732 KB
testcase_61 AC 86 ms
37,752 KB
testcase_62 AC 123 ms
92,388 KB
testcase_63 AC 122 ms
94,640 KB
testcase_64 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

template <class S, S (*op)(S, S), S (*e)()> struct SparseTable {
    int n;
    std::vector<int> floor_lg;
    std::vector<std::vector<S>> d;
    explicit SparseTable(std::vector<S>&& a): n(a.size()), floor_lg(n + 1) {
        if (n == 0) return;
        int frog = 0;
        for(int i = 1; i <= n; frog++) {
            int j = std::min(n + 1, 2 * i);
            for(; i < j; i++) floor_lg[i] = frog;
        }
        d.resize(frog);
        d[0] = std::move(a);
        for(int p = 0, w = 1; p < frog - 1; p++, w *= 2) {
            int last = n - 2 * w;
            d[p + 1].resize(last + 1);
            for(int i = 0; i <= last; i++) d[p + 1][i] = op(d[p][i], d[p][i + w]);
        }
    }
    explicit SparseTable(const std::vector<S>& a): SparseTable(std::vector<S>(a)) {}

    S query(int l, int r) {
        assert(0 <= l && l <= r && r <= n);
        if (l >= r) return e();
        int w = r - l;
        int p = floor_lg[w];
        return op(d[p][l], d[p][r - (1 << p)]);
    }
};

int op(int x, int y) { return min(x, y); }
int e() { return 1001001001;}

int op2(int x, int y) { return max(x, y);}
int e2() { return -1; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    string s, t;
    cin >> s >> t;
    constexpr int INF = 1001001001;
    int mx = 0;
    rep(_, 2) {
        int l = min(n, m);
        rep(i, l) {
            if (s[i] != t[i]) break;
            chmax(mx, i + 1);
        }
        reverse(all(s));
    }
    t += '$';
    rep(i, m) t += t[m - 1 - i];
    auto sa = suffix_array(t);
    VI sa_inv(2 * m + 1);
    rep(i, 2 * m + 1) sa_inv[sa[i]] = i;
    auto rmq = SparseTable<int, op, e>(lcp_array(t, sa));
    VI d(m + 1);
    for(int i = 1; i < m; i++) {
        int idx1 = sa_inv[i], idx2 = sa_inv[2 * m + 1 - i];
        if (idx1 > idx2) swap(idx1, idx2);
        d[i] = i + rmq.query(idx1, idx2);
    }
    d[m] = m;
    segtree<int, op2, e2> seg(d);
    int r = m;
    for(int t = 1;; t++) {
        int c = (r + 1) / 2;
        int nr = seg.max_right(c, [&](int x) { return x < r; });
        if (nr <= mx) {
            cout << t << endl;
            return 0;
        } else if (nr == r) {
            cout << -1 << endl;
            return 0;
        }
        r = nr;
    }
}
0