#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;cin>>n;
string s;cin>>s;
int j=0,z=0;
for(auto it:s)
{
if(it=='1')j++;
else z++;
}
z-=2*n;j-=3*n;
if(j<0||z<0)
{
cout<<"NIE\n";
return 0;
}
int a=0,g=0,c=j,o=0;
while(a+g+c+o>n)
{
if(c>1)
{
c-=2;
g++;
}
else if(c>0&&g>0)
{
c--;
g--;
o++;
}
else if(g>2)
{
g-=3;
o+=2;
}
else break;
}
a=n-c-g-o;
if(c+2*g+3*o==j&&3*a+2*c+g==z&&a>=0&&a+c+g+o==n)
{
while(c--)cout<<"c";
while(a--)cout<<"a";
while(g--)cout<<"g";
while(o--)cout<<"o";
cout<<"\n";
}
else
{
cout<<"NIE\n";
}
}
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<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n;cin>>n; string s;cin>>s; int j=0,z=0; for(auto it:s) { if(it=='1')j++; else z++; } z-=2*n;j-=3*n; if(j<0||z<0) { cout<<"NIE\n"; return 0; } int a=0,g=0,c=j,o=0; while(a+g+c+o>n) { if(c>1) { c-=2; g++; } else if(c>0&&g>0) { c--; g--; o++; } else if(g>2) { g-=3; o+=2; } else break; } a=n-c-g-o; if(c+2*g+3*o==j&&3*a+2*c+g==z&&a>=0&&a+c+g+o==n) { while(c--)cout<<"c"; while(a--)cout<<"a"; while(g--)cout<<"g"; while(o--)cout<<"o"; cout<<"\n"; } else { cout<<"NIE\n"; } } |
English