結果

問題 No.1743 Permutation Code
ユーザー noya2
提出日時 2021-09-20 11:49:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,657 bytes
コンパイル時間 2,671 ms
コンパイル使用メモリ 206,968 KB
最終ジャッジ日時 2025-01-24 16:01:19
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 TLE * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
using namespace std;

template<class T>ostream &operator<<(ostream &os,const vector<T> &v){if(v.size()==0){os<<endl;}else{rep(i,v.size())os<<v[i]<<(i+1==v.size()?"\n":" ");}return os;}

string bin(int n){
    string ans = "";
    while (n > 0){
        int m = n % 2;
        ans += to_string(m);
        n /= 2;
    }
    reverse(ans.begin(),ans.end());
    return ans;
}

int num(string s){
    reverse(s.begin(),s.end());
    int ans = 0;
    int ni = 1;
    for (char c : s){
        if (c == '1') ans += ni;
        ni *= 2;
    }
    return ans;
}

set<int> used;
stack<int> now;
string code;
int leng;
int lim;

void saiki(int ind){
    if (ind == leng){
        vector<int> ans;
        while (!now.empty()){
            ans.emplace_back(now.top());
            now.pop();
        }
        reverse(ans.begin(),ans.end());
        cout << ans;
        exit(0);
    }
    string nows = "";
    repp(i,leng,ind){
        nows += code[i];
        int nown = num(nows);
        if (nown > lim) break;
        if (i + 1 == leng || code[i+1] == '1'){
            if (used.find(nown) == used.end()){
                used.insert(nown);
                now.push(nown);
                saiki(i+1);
                used.erase(nown);
                now.pop();
            }
        }
    }
}

int main(){
    string s; cin >> s;
    int n = 0;
    int siz = s.size();
    int tiz = 0;
    while (tiz < siz){
        n++;
        string z = bin(n);
        tiz += z.size();
    }
    code = s;
    leng = siz;
    lim = n;
    saiki(0);
}
0