GDUT - 专题学习1 K - Equidistant Letters

K - Equidistant Letters

题目

You are given a string ss, consisting of lowercase Latin letters. Every letter appears in it no more than twice.

Your task is to rearrange the letters in the string in such a way that for each pair of letters that appear exactly twice, the distance between the letters in the pair is the same. You are not allowed to add or remove letters.

It can be shown that the answer always exists. If there are multiple answers, print any of them.

Input

The first line contains a single integer tt (1≤t≤10^3) — the number of testcases.

Each testcase consists of a non-empty string ss, consisting of lowercase Latin letters. Every letter appears in the string no more than twice. The length of the string doesn't exceed 52.

Output

For each testcase, print a single string. Every letter should appear in it the same number of times as it appears in string ss. For each pair of letters that appear exactly twice, the distance between the letters in the pair should be the same.

If there are multiple answers, print any of them.

Example

Input

3
oelhl
abcdcba
ac

Output

hello
ababcdc
ac

Note

In the first testcase of the example, the only letter that appears exactly twice is letter 'l'. You can rearrange the letters arbitrarily, since there are no distances to compare.

In the second testcase of the example, the letters that appear exactly twice are 'a', 'b' and 'c'. Initially, letters 'a' are distance 66 apart, letters 'b' are distance 44 apart and letters 'c' are distance 22 apart. They are not the same, so we have to rearrange the letters. After rearrangement, letters 'a' are distance 22 apart, letters 'b' are distance 22 apart and letters 'c' are distance 22 apart. They are all the same, so the answer is valid.

In the third testcase of the example, there are no letters that appear exactly twice. Thus, any rearrangement is valid. Including not changing the string at all.

大致题意

有一个长度不超过52且元素均为小写字母的字符串s,将其重新排序,使得字符串s中所有恰好出现过2次的字母其之间的距离都相等,并输入任意一个答案。

例如样例2 字符串 abcdcba 中a出现了2次,之间距离为6,b出现了2次,之间距离为4,c出现了2次,之间距离为2,重新排序后为ababcdc,其出现过2次的字母之间的距离都变成了2.

思路

正如题目中Note所描述的,如果字符串中只有1对或者0对出现过2次的字母,则无需变化直接输出即可。

但如果有2对以上呢?通过思考不难想到,我们只用将所有出现过2次的字母放在一块不就可以了吗?这样他们的距离都为0了。

综上所述,其实我们只需将字符串排个序就好了,十分简单。

代码

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int t;
string s;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> t;
	while (t--)
	{
		cin >> s;
		sort(s.begin(), s.end());
		cout << s << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值