fork download
  1. #include<stdio.h>
  2. int main()
  3. {
  4. long long int t,n,k,i,j,count,c,flag,c1,c2;
  5. scanf("%lld",&t);
  6. while(t--)
  7. {
  8. c=0;
  9. c1=0;
  10. c2=0;
  11. count=0;
  12. scanf("%lld%lld",&n,&k);
  13. char a[n+1];
  14. char b[n+1];
  15. scanf("%s",&a);
  16. for(i=0;i<n;i++)
  17. b[i]=a[i];
  18. if(k>1)
  19. {
  20. i=0;
  21. count=0;
  22. while(i<n-1)
  23. {
  24. count=0;
  25. while(a[i]==a[i+1])
  26. {
  27. count++;
  28. if(count>k)
  29. {
  30. c++;
  31. if(a[i]=='0')
  32. a[i]='1';
  33. else
  34. a[i]='0';
  35. count=0;
  36. }
  37. i++;
  38. }
  39. count++;
  40. if(count>k)
  41. {
  42. c++;
  43. if(a[i-1]=='0')
  44. a[i-1]='1';
  45. else
  46. a[i-1]='0';
  47. count=0;
  48. }
  49. i++;
  50. }
  51. if(a[n-1]==a[n])
  52. {
  53. count++;
  54. if(count>k)
  55. {
  56. c++;
  57. if(a[n-1]=='0')
  58. a[n-1]='1';
  59. else
  60. a[n-1]='0';
  61. count=0;
  62. }
  63. }
  64. printf("%lld\n",c);
  65. for(i=0;i<n;i++)
  66. printf("%c",a[i]);
  67. printf("\n");
  68. }
  69. else
  70. {
  71. i=0;
  72. while(i<n-1)
  73. {
  74. if(a[i]==a[i+1])
  75. {
  76. count++;
  77. if(a[i+1]=='0')
  78. a[i+1]='1';
  79. else
  80. a[i+1]='0';
  81. c1++;
  82. }
  83. i++;
  84. }
  85. i=n-1;
  86. while(i>0)
  87. {
  88. if(b[i]==b[i-1])
  89. {
  90. if(b[i-1]=='0')
  91. b[i-1]='1';
  92. else
  93. b[i-1]='0';
  94. c2++;
  95. }
  96. i--;
  97. }
  98. if(c1<=c2)
  99. {
  100. printf("%lld\n",c1);
  101. for(i=0;i<n;i++)
  102. printf("%c",a[i]);
  103. printf("\n");
  104. }
  105. else
  106. {
  107. printf("%lld\n",c2);
  108. for(i=0;i<n;i++)
  109. printf("%c",b[i]);
  110. printf("\n");
  111. }
  112. }
  113. }
  114. return 0;
  115. }
  116.  
Runtime error #stdin #stdout 0s 2052KB
stdin
Standard input is empty
stdout
Standard output is empty