Test instructions: Given the length of n 01 strings, any one 01 string cannot be the prefix of another string, which requires the output of these strings.
Start very confused, the main feeling is difficult to output, to determine whether there is a solution or very easy. Look at the official work, because the number of strings must be, you can put all the strings can be added to the queue, if the number is enough to stop joining. Then it is output in turn, did not pay attention to the order WA once,,, and then the code changed so ugly.
#include <iostream>#include<map>#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<vector>#include<queue>#include<stack>#definePB Push_backusing namespaceStd;typedefLong Longll;Const intmaxv=1005;intN;intA[maxv],b[maxv];queue<string>Q;vector<string>Ans;stack<string> xxx[1005];intMain () {CIN>>N; for(intI=0; i<n;i++) {scanf ("%d",&A[i]); B[i]=A[i]; } sort (A,a+N); intk=0; Q.push (string("")); inti; for(i=0; i<n;i++){ while(!Q.empty ()) { while(Q.front (). Size () <a[i]-1){ stringtt=Q.front (); Q.pop (); if(Q.size () <N) {Q.push (TT+'0'); Q.push (TT+'1'); } } stringnow=Q.front (); Q.pop (); if(Now.size () <a[i]-1|| Now.size () >a[i])Continue; if(Now.size () ==a[i]-1) {ANS.PB ( now+'1'); Xxx[a[i]].push ( now+'1'); Q.push ( now+'0'); Break; }Else if(now.size () = =A[i]) {ANS.PB (now); Xxx[a[i]].push (now); Break; } } if(Q.empty ()) Break; } if(Ans.size ()!=n) puts ("NO"); Else{puts ("YES"); for(intI=0; i<n;i++) {cout<<xxx[b[i]].top () <<Endl; Xxx[b[i]].pop (); } } return 0;}
View Code
Codeforces 37C (Miscellaneous questions)