結果

問題 No.1743 Permutation Code
ユーザー 👑 NachiaNachia
提出日時 2021-11-22 13:46:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,645 bytes
コンパイル時間 1,331 ms
コンパイル使用メモリ 102,204 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-06 05:10:18
合計ジャッジ時間 13,738 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,640 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// ちょっと減らして愚直DFS

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
//#include <atcoder/modint>
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(n); i++)

//using m32 = atcoder::static_modint<998'244'353>;


int msb_idx(u64 x){ return 63 - __builtin_clzll(x); }
int C_size_to_N(int C_size){
    int n = 0;
    int c_size_left = C_size;
    while(c_size_left > 0){
        n++;
        c_size_left -= msb_idx(n) + 1;
    }
    if(c_size_left != 0) return -1;
    return n;
}
string to_binstr(u64 n){
    string res;
    while(n){ res.push_back('0' + (n&1)); n>>=1; }
    reverse(res.begin(), res.end());
    return res;
}
u64 from_binstr(const string& s){
    u64 res = 0;
    for(char c : s) res = res * 2 + (c - '0');
    return res;
}


string C;
int N;

vector<string> code;
vector<set<int>> candid;
vector<vector<int>> length_pos_code;
vector<vector<int>> pos_code;

vector<int> used;
vector<int> ans;
void dfs(int p){
    if(ans.size() == N) return;
    for(int nx : pos_code[p]){
        if(used[nx]) continue;

        int nxp = p + code[nx].size();

        used[nx] = 1;
        ans.push_back(nx);
        dfs(nxp);
        if(ans.size() == N) return;
        ans.pop_back();
        used[nx] = 0;
    }
}


int main(){
    cin >> C;
    N = C_size_to_N(C.size());

    //cerr << "N = " << N << endl;
    code.resize(N);
    for(int i=1; i<=N; i++) code[i-1] = to_binstr(i);
    int max_code_len = code.back().size();

    length_pos_code.assign(max_code_len+1, vector<int>(C.size(), -1));
    candid.resize(N);
    for(int l=1; l<=max_code_len; l++){
        for(int p=0; p+l<=C.size(); p++){
            if(C[p] == '0') continue;
            if(C.size() != p+l) if(C[p+l] == '0') continue;
            int v = from_binstr(C.substr(p,l));
            if(v > N) continue;
            length_pos_code[l][p] = v-1;
            candid[v-1].insert(p);
        }
    }

    vector<int> list_det;
    for(int v=0; v<N; v++) if(candid[v].size() == 1) list_det.push_back(v);
    for(int vi=0; vi<list_det.size(); vi++){
        int v = list_det[vi];
        int p = *candid[v].begin();
        int l = code[v].size();
        for(int nxl=1; nxl<=max_code_len; nxl++){
            for(int nxp=max<int>(0,p-nxl+1); nxp<=min<int>(C.size()-1,p+l-1); nxp++){
                int nxv = length_pos_code[nxl][nxp];
                if(nxv == -1) continue;
                if(nxl == l && nxp == p) continue;
                candid[nxv].erase(nxp);
                length_pos_code[nxl][nxp] = -1;
                if(candid[nxv].size() == 1) list_det.push_back(nxv);
            }
        }
    }

    //for(int i=0; i<N; i++){
    //    cerr << "candid " << (i+1) << " :";
    //    for(auto p : candid[i]) cerr << " " << p;
    //    cerr << "\n";
    //    cerr << flush;
    //}

    pos_code.resize(C.size());
    for(int l=max_code_len; l>=1; l--){
        for(int p=0; p<C.size(); p++){
            if(length_pos_code[l][p] == -1) continue;
            pos_code[p].push_back(length_pos_code[l][p]);
        }
    }

    //for(int i=0; i<C.size(); i++){
    //    cerr << "pos_code " << (i+1) << " :";
    //    for(auto p : pos_code[i]) cerr << " " << p;
    //    cerr << "\n";
    //    cerr << flush;
    //}

    used.assign(N,0);
    dfs(0);
    
    rep(i,N){
        if(i) cout << " ";
        cout << (ans[i] + 1);
    }
    cout << "\n";

    return 0;
}




struct ios_do_not_sync {
    ios_do_not_sync() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0