結果

問題 No.3427 Erasing a Subsequence
コンテスト
ユーザー y_1
提出日時 2026-01-11 15:31:08
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
WA  
実行時間 -
コード長 3,744 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,979 ms
コンパイル使用メモリ 352,760 KB
実行使用メモリ 61,312 KB
最終ジャッジ日時 2026-01-11 15:31:13
合計ジャッジ時間 4,858 ms
ジャッジサーバーID
(参考情報)
judge5 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
#define ov4(a, b, c, d, name, ...) name
#define rep3(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep2(i, a, b) rep3(i, a, b, 1)
#define rep1(i, n) rep2(i, 0, n)
#define rep0(n) rep1(aaaaa, n)
#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define per(i, a, b) for(ll i = (a)-1; i >= (b); i--)
#define fore(e, v) for(auto&& e : v)
#define all(a) begin(a), end(a)
#define si(a) (int)(size(a))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define eb emplace_back

template<typename T, typename S> bool chmin(T& a, const S& b) { return a > b ? a = b, 1 : 0; }
template<typename T, typename S> bool chmax(T& a, const S& b) { return a < b ? a = b, 1 : 0; }

const int INF = 1e9 + 100;
const ll INFL = 3e18 + 100;

#define i128 __int128_t

template<typename T> vi z_algorithm(const vector<T>& s) {
   int n = si(s), l = -1, r = -1;
   vi z(n, n);
   rep(i, 1, n) {
      int& x = z[i] = i < r ? min<ll>(r - i, z[i - l]) : 0;
      while(i + x < n and s[i + x] == s[x]) x++;
      if(i + x > r) l = i, r = i + x;
   }
   return z;
}

int main() { 
    cin.tie(0)->sync_with_stdio(0); 

    ll n,m;
    cin >> n >> m;
    vl s(n),t(m);
    rep(i,n){
        cin >> s[i];
    }
    rep(i,m){
        cin >> t[i];
    }
    vector<vi> w(n);
    rep(i,n){
        vl temp;
        rep(j,i,n){
            temp.push_back(s[j]);
        }
        temp.push_back(-1);
        rep(j,m){
            temp.push_back(t[j]);
        }
        w[i] = z_algorithm(temp);
    }
    auto f = [&](ll i,ll j,ll k){
        if(k == 0){
            return true;
        }
        if(i == n){
            if(j == m){
                return true;
            }
            else{
                return false;
            }
        }
        if(w[i][n-i+1+j] >= k){
            return true;
        }
        else{
            return false;
        }
    };

    vl check(n+1,m);
    per(i,n-1,0){
        check[i] = check[i+1];
        if(check[i] > 0 && s[i] == t[check[i]-1]){
            check[i]--;
        }
    }

    map<ll,vl> mp;

    auto comp = [&](vl x,vl y){
        rep(i,min(x.size(), y.size())){
            if(x[i] < y[i]){
                return 1;
            }
            else if(x[i] > y[i]){
                return -1;
            }
        }
        return 0;
    };

    auto calc = [&](auto calc,ll idx,ll jdx) -> vl {
        //cerr << idx << ' ' << jdx << endl;
        if(idx >= n){
            return {};
        }
        
        if(mp.count(idx * (n+1) + jdx) == 1){
            return mp[idx * (n+1) + jdx];
        }
        ll length = 0;
        vl temp = {1LL<<60};
        rep(i,idx,n+1){
            //cerr << idx << ' ' << jdx << ' ' << i << ' ' << f(idx,jdx,length) << ' ' << (check[i] <= jdx+i) << endl;
            if(f(idx,jdx,length) == true && check[i] <= jdx+i && jdx+length <= m){
                vl to = calc(calc,i+1,jdx+length);
                if(i < n)to.insert(to.begin(),s[i]);
                /*
                for(ll z:to){
                    cerr << z << ' ';
                }
                cerr << endl;
                */
                if(comp(temp,to) != 1){
                    swap(temp,to);
                }
            }
            length++;
        }
        mp[idx * (n+1) + jdx] = temp;
        /*
        cerr <<'!'<< idx << ' ' << jdx << ' ';
        for(ll x:temp){
            cerr << x << ' ';
        }
        cerr << endl;
        */
        return temp;
    };

    auto ans = calc(calc,0,0);
    for(ll x:ans){
        cout << x << ' ';
    }
    cout << endl;
    

    
}
0