-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathA_AB_Balance.cpp
More file actions
55 lines (52 loc) · 1.12 KB
/
A_AB_Balance.cpp
File metadata and controls
55 lines (52 loc) · 1.12 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
#include <iostream>
using namespace std;
typedef long long ll;
int t;
string s;
int cnt(string a, string b){
int leng = b.length(), res = 0;
for(int i=0;i<a.length()-leng;i++) {
if(a.substr(i, leng) == b) res++;
}
return res;
}
int main() {
cin >> t;
while(t--){
cin >> s;
int ab = cnt(s, "ab");
int ba = cnt(s, "ba");
if(ab > ba)
{
int ind = 0;
while(ab > ba)
{
if(s[ind] == 'a' && s[ind+1] == 'b')
{
s[ind++] = 'b';
ab--;
}
else if(s[ind] == 'a' && s[ind+1] == 'a')
{
s[ind++] = 'b';
ba++;
}
ind++;
}
}
else
{
int ind = 0;
while(ba > ab)
{
if(s[ind] == 'b' && s[ind+1] == 'a')
{
s[ind] = 'a';
ba--;
}
ind++;
}
}
cout << s << endl;
}
}