AtCoder Regular Contest 113
E
Rvom and Rsrev
AtCoder
⌀ 2789
原题
← DF →
翻译中…

Problem Statement

翻译中…

Given is a string $S$ consisting of a and b. Find the lexicographically greatest string that can be obtained by applying the following operation to $S$ zero or more times:

翻译中…
  • Choose two characters of $S$ that are the same letter. Reverse the string between them and delete the chosen two characters. In other words: let $s_i$ denote the $i$-th character of $S$. Choose $i < j$ such that $s_i = s_j$ and replace $S$ with $s_1\dots s_{i-1}s_{j-1}\dots s_{i+1}s_{j+1}\dots s_{|S|}$.
翻译中…

In this problem, you are given $T$ test cases. The $i$-th test case is represented as $S_i$ and asks you to solve the problem above for $S = S_i$.

翻译中…

Constraints

翻译中…
  • $1 \leq T \leq 2\times 10^5$
  • $1 \leq |S_i|\quad (i=1,\dots, T)$
  • $1 \leq |S_1| + \dots + |S_T| \leq 2\times 10^5$
  • $S_i$ consists of a and b.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

T
S_1
\vdots
S_T

翻译中…

Output

翻译中…

Print $T$ lines. The $i$-th line should contain the lexicographically greatest string that can be obtained by applying the operation to $S_i$ zero or more times.

翻译中…

样例

Input #1
20
abbaa
babbb
aabbabaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbabaaaaabbaababaaabbabbbbbaaaaa
babbbaaaabaababbbabaabaaaaababaa
bbaababababbbaaabaabababaabbabab
abaabbabaabaaaaabaaaabbaabaaaaab
aabababbabbabbabbaaaabbabbbabaab
aabababbabbbbaaaabbaaaaabbaaaabb
abbbbaabaaabababaaaababababbaabb
aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbb
aaaaaaaaaabbbbbbbbbbbbbbbbbbbbbb
abababaaababaaabbbbbaaaaaaabbbbb
aabbaaaaababaabbbbbbbbbaabaaabbb
babababbababbbababbbbbababbbabbb
bbbbababbababbbabababbabbabbbbbb
aaaaaaaaaaaaaaaaababababbbabbbbb
aabababbabbabababababababbbbabbb
Output #1
bba
bba
bbba
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbaaaaaaaa
bbbbbbbbbbbbbaaaaaaa
bbbbbbbbbbbbbbbb
bbbbbbbbbb
bbbbbbbbbbbbbbbbab
bbbbbbbbbbbbbb
bbbbbbbbbbbbbabb
abbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbaaaaaaaaa
bbbbbbbbbbbbbbbaaaaa
bbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbba
bbbbbbbbbaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbba
- In the $1$-st test case, we can do the operation for the $1$-st and $4$-th characters to make $S$ `bba`. - In the $2$-nd test case, we can do the operation for the $1$-st and $5$-th characters to make $S$ `bba`. - In the $3$-rd test case, we can do the operation for the $1$-st and $2$-nd characters to make $S$ `bbabaa`, then do it for the $3$-rd and $5$-th characters to make $S$ `bbba`.
题目信息
题号
arc113_e
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。