-
Notifications
You must be signed in to change notification settings - Fork 0
/
DEVSTR.cpp
93 lines (88 loc) · 1.18 KB
/
DEVSTR.cpp
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
#include <bits/stdc++.h>
using namespace std;
char flip(char c)
{
if(c == '0')
return '1';
return '0';
}
int main()
{
int t,n,k,ct,chg,minv;
char ch;
string str;
cin>>t;
while(t--)
{
cin>>n>>k;
cin>>str;
int l = str.length();
chg = 0;
if(k==1)
{
ch = '0';
for(int i=0 ; i<l ; i++)
{
if(str[i] != ch)
chg++;
ch = flip(ch);
}
minv = chg;
chg = 0;
ch = '1';
for(int i=0 ; i<l ; i++)
{
if(str[i] != ch)
chg++;
ch = flip(ch);
}
cout<<min(chg , minv)<<endl;
if(chg<minv)
{
ch = '1';
for(int i=0 ; i<l ; i++)
{
cout<<ch; ch = flip(ch);
}
}
else
{
ch = '0';
for(int i=0 ; i<l ; i++)
{
cout<<ch; ch = flip(ch);
}
}
cout<<endl;
continue;
}
ct = 1; ch = str[0];
for(int i=1 ; i<l ; i++)
{
if(str[i] != str[i-1])
{
ct = 1; ch = str[i];
}
else
{
ct++;
if(ct > k)
{
if(i!=l-1 && str[i+1] != str[i])
{
str[i-1] = str[i+1];
ct = 0;
}
else
{
str[i] = flip(str[i]);
ct = 0;
}
chg++;
}
}
// cout<<i<<" : "<<ct<<endl;
}
cout<<chg<<endl<<str<<endl;
}
}