Problem Description
*Those blood-soaked shores of Kalimdor is like a ghost haunting Jaina Proudmoore ever since the day she pushed her father into hell.*
*Now, standing in front of the devastated ruins of Theramore, she knew how naive she had been to want peace.*
*The Focusing Iris. It was the most brutal and cowardly killing method Jaina could imagine.*
*The Horde wants war. They will do anything to destroy us. But if this is all they want, Jaina will be pleased to offer them a big one.*
The warships of the Horde can be described as a string s which contains only '0' and '1', denoting the small warship and the large warship. Jaina can perform some magic to the string. In one magic, she chooses an arbitrary interval with odd length and reverse it. Jaina can perform this magic as many times as she likes.
Jaina wants the small warships to be in the front, since they are easier to destroy. She asks for your help, and you need to tell her the lexicographically smallest string that she can obtain.
Note: in this problem, suppose two sequences s and t both have length n, then s is lexicographically smaller than t if there exists a position i(1≤i≤n) such that sj=tj for all 1≤j<i and si=′0′,ti=′1′.
Input
The input consists of multiple test cases.
The first line contains an integer T (1≤T≤10) denoting the number of test cases.
Each test case consists of only one line containing a string s (|s|≤10^5).
Output
Output one line containing the lexicographically smallest string that you can get.
Sample Input
2 101001 01101100000000101010
Sample Output
001011 00000000001010101111
Hint
In the first test case, Jaina performs magic to the interval [3,5] and the string is changed to 100011. Then Jaina performs magic to the interval [1,3] and the string is changed to 001011.
题解:偶数位和奇数位分别字典序最小即可,主要是aa++和bb++技巧。
#include<bits/stdc++.h>
using namespace std;
string s;
string a,b;
typedef long long ll;
ll aa,bb;
ll A,B;
int main()
{
int _;
cin>>_;
while(_--)
{
aa=0,bb=0;
A=0,B=0;
cin>>s;
ll len=s.size();
//cout<<len<<endl;
for(ll i=0;i<len;i++)
{
if(i%2)//偶数位
{
//cout<<"A"<<endl;
if(s[i]=='0')
{
aa++;
//cout<<"aa"<<endl;
}
A++;
}
else
{
//cout<<"B"<<endl;
if(s[i]=='0')
{
bb++;
//cout<<"bb"<<endl;
}
B++;
}
}
//cout<<"aa="<<aa<<" "<<"bb="<<bb<<endl;
for(ll i=0;i<aa;i++)
{
s[i*2+1]='0';
}
for(ll i=aa;i<A;i++)
{
s[i*2+1]='1';
}
for(ll i=0;i<bb;i++)
{
s[i*2]='0';
}
for(ll i=bb;i<B;i++)
{
s[i*2]='1';
}
cout<<s<<endl;
}
return 0;
}