#include <iostream> using namespace std; bool isValid(int n, int one){//n to ilość znaków if(n==0 && one==0) return true; return (3*n<8*one && 6*n>one*8); } bool generate(int one, int n, string &odp){ // n to ilość bloków for(int i=0;i<n;i++){ //cout<<"n"<<n<<" one"<<one<<endl; if(isValid(8*(n-1-i),one-3)){ odp+="a"; one-=3; continue; } if(isValid(8*(n-1-i),one-4)){ odp+="c"; one-=4; continue; } if(isValid(8*(n-1-i),one-5)){ odp+="g"; one-=5; continue; } if(isValid(8*(n-1-i),one-6)){ odp+="w"; one-=6; continue; } return false; } return true; } int main(){ int n,ones=0,zero=0; cin>>n; string temp; cin>>temp; for(char c : temp){ if(c=='1') ones++; else zero++; } string odp=""; if(generate(ones,n,odp)) cout<<odp<<endl; else cout<<"NIE"<<endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <iostream> using namespace std; bool isValid(int n, int one){//n to ilość znaków if(n==0 && one==0) return true; return (3*n<8*one && 6*n>one*8); } bool generate(int one, int n, string &odp){ // n to ilość bloków for(int i=0;i<n;i++){ //cout<<"n"<<n<<" one"<<one<<endl; if(isValid(8*(n-1-i),one-3)){ odp+="a"; one-=3; continue; } if(isValid(8*(n-1-i),one-4)){ odp+="c"; one-=4; continue; } if(isValid(8*(n-1-i),one-5)){ odp+="g"; one-=5; continue; } if(isValid(8*(n-1-i),one-6)){ odp+="w"; one-=6; continue; } return false; } return true; } int main(){ int n,ones=0,zero=0; cin>>n; string temp; cin>>temp; for(char c : temp){ if(c=='1') ones++; else zero++; } string odp=""; if(generate(ones,n,odp)) cout<<odp<<endl; else cout<<"NIE"<<endl; return 0; } |