-
Notifications
You must be signed in to change notification settings - Fork 7
/
ABString.cpp
261 lines (234 loc) · 6.08 KB
/
ABString.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
A. AB Balance
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s of length n consisting of characters a and/or b.
Let AB(s) be the number of occurrences of string ab in s as a substring. Analogically, BA(s) is the number of occurrences of ba in s as a substring.
In one step, you can choose any index i and replace si with character a or b.
What is the minimum number of steps you need to make to achieve AB(s)=BA(s)?
Reminder:
The number of occurrences of string d in s as substring is the number of indices i (1≤i≤|s|−|d|+1) such that substring sisi+1…si+|d|−1 is equal to d. For example, AB(aabbbabaa)=2 since there are two indices i: i=2 where aabbbabaa and i=6 where aabbbabaa.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). Description of the test cases follows.
The first and only line of each test case contains a single string s (1≤|s|≤100, where |s| is the length of the string s), consisting only of characters a and/or b.
Output
For each test case, print the resulting string s with AB(s)=BA(s) you'll get making the minimum number of steps.
If there are multiple answers, print any of them.
Example
inputCopy
4
b
aabbbabaa
abbb
abbaab
outputCopy
b
aabbbabaa
bbbb
abbaaa
Note
In the first test case, both AB(s)=0 and BA(s)=0 (there are no occurrences of ab (ba) in b), so can leave s untouched.
In the second test case, AB(s)=2 and BA(s)=2, so you can leave s untouched.
In the third test case, AB(s)=1 and BA(s)=0. For example, we can change s1 to b and make both values zero.
In the fourth test case, AB(s)=2 and BA(s)=1. For example, we can change s6 to a and make both values equal to 1.
*/
#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <chrono>
#include <complex>
using namespace std;
#define ll long long
#define ld long double
#define ui unsigned int
#define ull unsigned ll
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define hashmap unordered_map
#define hashset unordered_set
#define lb lower_bound
#define ub upper_bound
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define ff first
#define ss second
#define foi(n) for(ll i=0;i<n;i++)
#define foj(n) for(ll j=0;j<n;j++)
#define fok(n) for(ll k=0;k<n;k++)
#define forr(i,a,b) for(ll i=a;i<b;i++)
#define forrr(i,b,a) for(ll i=b;i>=a;i--)
#define forrrr(i,a,b,k) for(ll i=a;i<b;i=i+k)
#define graph vector<vector<ll>>
#define sz(v) v.size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef pair< ll, pll> plll;
typedef queue<ll> qll;
typedef vector<plll> vplll;
typedef vector<set<ll>> vsll;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef map<string, int> msi;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<ll, vll> mvll;
typedef map<vll, ll> mvlll;
typedef map<char, ll> mcl;
typedef map<pair<ll, ll>, ll> mplll;
typedef unordered_map<char, ll> umcl;
typedef unordered_map< ll, char> umlc;
typedef unordered_map< ll, ld> umld;
typedef map<int, string> mis;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
typedef priority_queue <ll> pq;
typedef priority_queue<pii, vector<pii>, greater<pii> > pqq;
typedef priority_queue<ll, vector<ll>, greater<ll>> prq;
const ll MOD = 1000000007;
const ll modx = 998244353;
ld PI = 3.1415926535897;
const ll N = 200010;
void solve();
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("error1.txt", "w", stderr);
freopen("output1.txt", "w", stdout);
#endif
ll t ; cin >> t;
while (t--)
{
solve();
cout << "\n";
}
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
return 0;
}
ll ceils(ll x, ll y) {
return x / y + ((x % y) != 0);
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
ll powmod(ll x, ll y) {
ll res = 1;
for (ll i = 0; i < y; i++) {
res = res * x % MOD;
}
return res;
}
bool isPrime(ll n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ll i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
bool COMP(pll l, pll r) {
return l.ss < r.ss;
}
ll kadanesAlgo(vll a)
{
ll n = a.size();
ll currMax = 0;
ll mx = INT_MIN;
foi(n)
{
currMax += a[i];
if (currMax <= a[i])
{
currMax = a[i];
}
mx = max(currMax, mx);
}
return mx;
}
ll ask(ll x, ll y, ll n) {
cout << "?" << " ";
foi(n - 1) {
cout << x << " ";
}
cout << y << endl;
ll s;
cin >> s;
return s;
}
ll binpow(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
bool is_sorted(vector<ll>a) {
ll x = 0;
for (ll i = 0; i < a.size() - 1; i++) {
if (a[i] < a[i + 1])
x++;
}
return x == a.size() - 1;
}
bool check(string a, string b) {
ll n = a.size(), m = b.size();
if (a < b) {
return true;
}
if (a > b) {
return false;
}
foi(a.size()) {
if (a[i] > b[i]) {
return false;
}
else if (a[i] < b[i]) {
return true;
}
}
return false;
}
bool isSubSequence(string a, string b, ll m, ll n) {
ll j = 0;
for (ll i = 0; i < n and j < m; i++)
if (a[j] == b[i])
j++;
return (j == m);
}
void solve() {
string s;
cin >> s;
s[s.size() - 1] = s[0];
cout << s;
}