-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFebruary_Long_2022_I_BINBASBASIC.cpp
102 lines (86 loc) · 2.41 KB
/
February_Long_2022_I_BINBASBASIC.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
94
95
96
97
98
99
100
101
102
// CodeChef Februsry Long challenge 2022 I Division 3 (Rated)
// Problem Vivision 03
// 1 * Coder in CodeChef Till 06/02/2022
// Problem Name: Binary Base Basics
// Problem Code: BINBASBASIC
// Problem Link: https://www.codechef.com/FEB221C/problems/BINBASBASIC
/*
You are given a binary string S and an integer K.
In one operation, you can pick any bit and flip it,
i.e turn 0 to 1 or 1 to 0. Can you make the string S a palindrome using exactly K operations?
Print YES if this is possible, and NO if it is not.
------Input Format-------
The first line of input contains one integer T, denoting the number of test cases.
The description of T test cases follows.
Each test case consists of two lines of input.
The first line of each test case contains two space-separated integers N and K,
denoting the length of the binary string and the number of operations to be performed.
The second line contains the binary string S.
-------Output Format------
For each test case, print the answer on a new line — YES if the S can be
made a palindrome using exactly K operations, and NO otherwise.
You may print each character of YES and NO in either uppercase or
lowercase (for example, yes, yEs, Yes will all be considered identical).
Constraints
1≤T≤1000
1≤N≤1000
0≤K≤N
S is a binary string, i.e, contains only characters 0 and 1
Subtasks
Subtask #1 (100 points): Original constraints
Sample Input 1
2
3 0
110
6 1
101100
Sample Output 1
NO
YES
Explanation
Test case 1: S is not a palindrome, and no operations can be performed on it because K=0.
Test case 2: Flip the first bit to obtain S=001100, which is a palindrome.
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int t,k;
cin>>t>>k;
string s;
cin>>s;
int left=0;
int right=s.length()-1;
int count=0;
for(left;left<right;left++,right--)
{
if(s[left] != s[right])
{
count++;
}
}
if(count<=k)
{
if(s.length()%2==1)
{
cout<<"YES"<<endl;
}else
{
if((k-count)%2==0)
{
cout<<"YES"<<endl;
}else
{
cout<<"NO"<<endl;
}
}
}else
{
cout<<"NO"<<endl;
}
}
}