fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4. #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
  5. int main()
  6. {
  7. fast;
  8. int tc,n,k;
  9. string str;
  10. cin>>tc;
  11. while(tc--){
  12. cin>>n>>k>>str;
  13. int one=0,zero=0;
  14. for(int i=0;i<str.length();i++){
  15. if(str[i]=='0')
  16. zero++;
  17. else if(str[i]=='1')
  18. one++;
  19. }
  20. int temp=n/k;
  21. if(zero%temp!=0 || one%temp!=0){
  22. cout<<"IMPOSSIBLE"<<endl;
  23. continue;
  24. }
  25. zero=zero/temp;
  26. one=one/temp;
  27. int i=0,b=2*k-1;
  28. string ans=str;
  29. while(zero!=0){
  30. ans[i]='0';
  31. ans[b-i]='0';
  32. i++;
  33. zero--;
  34. }
  35. while(one!=0){
  36. ans[i]='1';
  37. ans[b-i]='1';
  38. i++;
  39. one--;
  40. }
  41. b+=k;
  42. int curr=0;
  43. while(b<n){
  44. ans[b-curr]=ans[i];
  45. i++;
  46. curr++;
  47. if(curr==k){
  48. curr=0;
  49. b+=k;
  50. }
  51. }
  52. cout<<ans<<endl;
  53. }
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 4384KB
stdin
2
8 2
00011101
6 2
100111
stdout
01100110
IMPOSSIBLE