-
Notifications
You must be signed in to change notification settings - Fork 30
/
Alternating String.cpp
90 lines (73 loc) · 2.74 KB
/
Alternating String.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
/*
Solution by Rahul Surana
***********************************************************
A binary string is called alternating if no two adjacent characters of the string are equal.
Formally, a binary string T of length M is called alternating if Ti≠Ti+1 for each 1≤i<M.
For example, 0, 1, 01, 10, 101, 010, 1010 are alternating strings while 11, 001, 1110 are not.
You are given a binary string S of length N.
You would like to rearrange the characters of S such that the length of the longest alternating substring of S is maximum.
Find this maximum value.
A binary string is a string that consists of characters 0 and 1.
A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all)
characters from the beginning and several (possibly, zero or all) characters from the end.
Input Format
The first line of input contains an integer T, denoting the number of test cases. The T test cases then follow:
The first line of each test case contains an integer N.
The second line of each test case contains the binary string S.
Output Format
For each test case, output the maximum possible length of the longest alternating substring of S after rearrangement.
***********************************************************
*/
#include <bits/stdc++.h>
#define ll long long
#define vl vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define all(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a) for(int i = 0; i < a; i++)
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define trace2(x,y) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define trace3(x,y,z) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
int main()
{
fast_io;
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
string a;
cin >> a;
int e= 0,o=0;
for(int i = 0; i < n; i++){
if(a[i] == '1'){
e++;
}
else{
o++;
}
}
ll ans = min(e,o)*2;
if(e> o){
ans = 2*o+1;
}
else if(e< o){
ans = 2*e+1;
}
else{
ans = 2*o;
}
if(ans == a.length()-1 && a.length()%2==1) ans = a.length();
if(ans == 0) ans = 1;
cout << ans <<"\n";
// cout << ans <<"\n";
}
}