Educational Codeforces Round 116 (Rated for Div. 2) A. AB Balance

题目链接: Problem - 1606A - Codeforces

You are given a string s of length nn consisting of characters a and/or b.

Let AB(s) be the number of occurrences of string ab in ss as a substring. Analogically, BA(s) is the number of occurrences of ba in ss as a substring.

In one step, you can choose any index i and replace sisi 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 dd in ss as substring is the number of indices ii (1≤i≤|s|−|d|+11≤i≤|s|−|d|+1) such that substring sisi+1…si+|d|−1sisi+1…si+|d|−1 is equal to dd. For example, AB(AB⁡(aabbbabaa)=2)=2 since there are two indices ii: i=2i=2 where aabbbabaa and i=6i=6 where aabbbabaa.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1000). Description of the test cases follows.

The first and only line of each test case contains a single string ss (1≤|s|≤100, where |s||s| is the length of the string ss), consisting only of characters a and/or b.

Output

For each test case, print the resulting string ss with AB⁡(s)=BA⁡(s) you'll get making the minimum number of steps.

If there are multiple answers, print any of them.

Example

input

Copy

4
b
aabbbabaa
abbb
abbaab

output

Copy

b
aabbbabaa
bbbb
abbaaa

Note

In the first test case, both AB(s)=0AB⁡(s)=0 and BA(s)=0BA⁡(s)=0 (there are no occurrences of ab (ba) in b), so can leave ss untouched.

In the second test case, AB(s)=2AB⁡(s)=2 and BA(s)=2BA⁡(s)=2, so you can leave ss untouched.

In the third test case, AB(s)=1AB⁡(s)=1 and BA(s)=0BA⁡(s)=0. For example, we can change s1s1 to b and make both values zero.

In the fourth test case, AB(s)=2AB⁡(s)=2 and BA(s)=1BA⁡(s)=1. For example, we can change s6s6 to a and make both values equal to 11.

题意:可以把字符串任意位置上的字母改成a或者b问用最小的方法求出ab,和ba数量相等的字符串

思路:只要字符串的头尾相同,那么字符串中ab,ba的数量就会相同

aabbbabaa 可以看出 aa abbba aba a

abbbbabbabbababaa 看成 abbbba abba abba aba aba aa

所以我们只需要判断字符串的头尾就可以了,只要头尾相同,中间ab,ba肯定相同

#include<bits/stdc++.h>
using namespace std;


int main(){
	int t;
	cin >> t;
	string s;
	while(t--){
		cin >> s;
		int len = s.length();
		if(s[0] == s[len - 1]){
			cout << s << endl;
		}else{
			s[len - 1] = s[0];
			cout << s << endl; 
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值