結果

問題 No.2162 Copy and Paste 2
ユーザー t98slidert98slider
提出日時 2022-12-25 20:51:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,066 ms / 7,000 ms
コード長 3,609 bytes
コンパイル時間 3,301 ms
コンパイル使用メモリ 178,908 KB
実行使用メモリ 23,956 KB
最終ジャッジ日時 2023-08-12 11:10:00
合計ジャッジ時間 15,835 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 60 ms
14,116 KB
testcase_07 AC 63 ms
13,608 KB
testcase_08 AC 305 ms
21,576 KB
testcase_09 AC 182 ms
13,892 KB
testcase_10 AC 204 ms
13,568 KB
testcase_11 AC 321 ms
18,424 KB
testcase_12 AC 321 ms
15,772 KB
testcase_13 AC 583 ms
21,764 KB
testcase_14 AC 223 ms
15,000 KB
testcase_15 AC 261 ms
14,984 KB
testcase_16 AC 252 ms
15,032 KB
testcase_17 AC 813 ms
21,548 KB
testcase_18 AC 886 ms
23,812 KB
testcase_19 AC 835 ms
22,104 KB
testcase_20 AC 755 ms
22,176 KB
testcase_21 AC 407 ms
21,564 KB
testcase_22 AC 400 ms
22,024 KB
testcase_23 AC 18 ms
11,548 KB
testcase_24 AC 1,066 ms
23,940 KB
testcase_25 AC 1,061 ms
23,948 KB
testcase_26 AC 952 ms
23,900 KB
testcase_27 AC 1,025 ms
23,956 KB
testcase_28 AC 773 ms
17,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T> std::vector<int> z_algorithm(const std::vector<T>& s) {
    int n = int(s.size());
    if (n == 0) return {};
    std::vector<int> z(n);
    z[0] = 0;
    for (int i = 1, j = 0; i < n; i++) {
        int& k = z[i];
        k = (j + z[j] <= i) ? 0 : std::min(j + z[j] - i, z[i - j]);
        while (i + k < n && s[k] == s[i + k]) k++;
        if (j + z[j] < i + z[i]) j = i;
    }
    z[0] = n;
    return z;
}
std::vector<int> z_algorithm(const std::string& s) {
    int n = int(s.size());
    std::vector<int> s2(n);
    for (int i = 0; i < n; i++) {
        s2[i] = s[i];
    }
    return z_algorithm(s2);
}

//双対セグメント木
template <class S,S (*mapping)(S, S),S (*id)()> struct dual_segtree {
    public:
        dual_segtree() : dual_segtree(0) {}
        dual_segtree(int n) : dual_segtree(std::vector<S>(n, id())) {}
        dual_segtree(const std::vector<S>& v) : _n(int(v.size())) {
            log = ceil_pow2(_n);
            size = 1 << log;
            d = std::vector<S>(2 * size, id());
            for (int i = 0; i < _n; i++) d[size + i] = v[i];
        }
        const S& operator[](int p) const {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--) push(p >> i);
            return d[p];
        }
        S& operator[](int p) { 
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--) push(p >> i);
            return d[p];
        }
        void apply(int p, S f) {
            assert(0 <= p && p < _n);
            p += size;
            for (int i = log; i >= 1; i--) push(p >> i);
            d[p] = mapping(f, d[p]);
        }
        void apply(int l, int r, S f) {
            assert(0 <= l && l <= r && r <= _n);
            if (l == r) return;
            l += size;
            r += size;
            for (int i = log; i >= 1; i--) {
                if (((l >> i) << i) != l) push(l >> i);
                if (((r >> i) << i) != r) push((r - 1) >> i);
            }
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
        }
    private:
        int _n, size, log;
        std::vector<S> d;
        void all_apply(int k, S f) {
            d[k] = mapping(f, d[k]);
        }
        void push(int k) {
            all_apply(2 * k, d[k]);
            all_apply(2 * k + 1, d[k]);
            d[k] = id();
        }
        int ceil_pow2(int n) {
            int x = 0;
            while ((1U << x) < (unsigned int)(n)) x++;
            return x;
        }
};
//xにfを作用させた時の値の変化
int op(int f, int x){return max(x, f);}
//作用させても変化させないもの
int id(){return 0;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    cin >> s;
    int n = s.size();
    auto za = z_algorithm(s);
    vector<vector<int>> tb(n);
    set<int> S;
    for(int i = 0; i < n; i++){
        za[i] = min(za[i], i);
        if(za[i] >= 2){
            tb[za[i]].push_back(i);
            S.insert(i);
        }
    }
    S.insert(n + 1);
    dual_segtree<int, op, id> seg(n + 1);
    for(int i = 2; i < n; i++){
        int cnt = seg[i] + i - 2;
        int pos = *S.begin() + i;
        while(pos <= n){
            seg.apply(pos, n + 1, cnt);
            cnt += i - 1;
            pos = *S.lower_bound(pos) + i;
        }
        for(auto &&p:tb[i]) S.erase(p);
    }
    cout << n - seg[n] << '\n';
}
0