-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevuAndBinaryString.java
More file actions
168 lines (146 loc) · 4.38 KB
/
DevuAndBinaryString.java
File metadata and controls
168 lines (146 loc) · 4.38 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* Created by hardCode on 4/5/2017.
* problem link :- https://www.codechef.com/MAY15/problems/DEVSTR
*/
public class DevuAndBinaryString {
static char[]a=new char[1000001];
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t,n,k;
String[]s;
String s1;
t=Integer.parseInt(br.readLine());
while (t-->0){
s=br.readLine().split("\\s");
n = Integer.parseInt(s[0]);
k = Integer.parseInt(s[1]);
s1=br.readLine();
for (int i = 0; i < n; i++) {
if ("1".equals(s1.charAt(i)+""))
a[i] = '1';
else
a[i]='0';
}
solve(a, n, k);
}
}
private static void solve(char[] a, int n, int k) {
if (k==1){
int temp1=zeroStart(a,n);//Flips required to create an alternating sequence starting from 0
int temp2=oneStart(a, n);//Flips required to create an alternating sequence starting from 1
StringBuilder sbr=new StringBuilder();
if (temp1<=temp2){
System.out.println(temp1);
for (int i = 0; i < n; i++) {
if ((i&1)==1){
sbr.append(1);
}
else {
sbr.append(0);
}
}
System.out.println(sbr.toString());
}
else {
System.out.println(temp2);
for (int i = 0; i < n; i++) {
if ((i&1)==1){
sbr.append(0);
}
else {
sbr.append(1);
}
}
System.out.println(sbr.toString());
}
return;
}
int sum=0,zeroCount=0,oneCount=0,start,end;
if (a[0]=='0')zeroCount=1;
else oneCount=1;
if (a[n-1]=='1')a[n]='0';
else a[n]='1';
for (int i = 1; i <=n; i++) {
if (a[i]=='1'){
oneCount++;
if (a[i]!=a[i-1]){
end=i-1;
start=end-zeroCount+1;
flipBits(a,start,end,k);
sum+=zeroCount/(k+1);
zeroCount=0;
}
}
else {
zeroCount++;
if (a[i]!=a[i-1]){
end=i-1;
start=end-oneCount+1;
flipBits(a, start, end,k);
sum+=oneCount/(k+1);
//System.out.println("oneCount "+oneCount+" start "+start+" end "+end+" i "+i);
oneCount=0;
}
}
}
System.out.println(sum);
for (int i = 0; i < n; i++) {
System.out.print(a[i]);
}
System.out.println();
}
private static int oneStart(char[] a, int n) {
int flips=0;
for (int i = 0; i < n; i++) {
if ((i&1)==1){
if (a[i]=='1')
flips++;
}
else {
if (a[i]=='0')
flips++;
}
}
return flips;
}
private static int zeroStart(char[] a, int n) {
int flips=0;
for (int i = 0; i < n; i++) {
if ((i&1)==1){
if (a[i]=='0')
flips++;
}
else {
if (a[i]=='1')
flips++;
}
}
return flips;
}
private static void flipBits(char[] a, int start, int end, int k) {
if (start == end) {
return;
}
int length = end - start + 1;
boolean flag = false;
if ((length % (k + 1)) == 0) {
flag = true;
flipBit(a, end);
}
for (int i = start + k; i <= end; i += k + 1) {
flipBit(a, i);
}
if (flag)
flipBit(a, end - 1);
}
private static void flipBit(char[]a,int i){
if (a[i]=='1')
a[i] = '0';
else a[i]='1';
}
}