题目描述
Lee was cleaning his house for the party when he found a messy string under the carpets. Now he’d like to make it clean accurately and in a stylish way…
The string s he found is a binary string of length n (i. e. string consists only of 0-s and 1-s).
In one move he can choose two consecutive characters si and si+1, and if si is 1 and si+1 is 0, he can erase exactly one of them (he can choose which one to erase but he can’t erase both characters simultaneously). The string shrinks after erasing.
Lee can make an arbitrary number of moves (possibly zero) and he’d like to make the string s as clean as possible. He thinks for two different strings x and y, the shorter string is cleaner, and if they are the same length, then the lexicographically smaller string is cleaner.
Now you should answer t test cases: for the i-th test case, print the cleanest possible string that Lee can get by doing some number of moves.
Small reminder: if we have two strings x and y of the same length then x is lexicographically smaller than y if there is a position i such that x1=y1, x2=y2,…, xi−1=yi−1 and xi<yi.
Input
The first line contains the integer t (1≤t≤104) — the number of test cases.
Next 2t lines contain test cases — one per two lines.
The first line of each test case contains the integer n (1≤n≤105) — the length of the string s.
The second line contains the binary string s. The string s is a string of length n which consists only of zeroes and ones.
It’s guaranteed that sum of n over test cases doesn’t exceed 105.
Output
Print t answers — one per test case.
The answer to the i-th test case is the cleanest string Lee can get after doing some number of moves (possibly zero).
Example
input
5
10
0001111111
4
0101
8
11001101
10
1110000000
1
1
output
0001111111
001
01
0
1
Note
In the first test case, Lee can’t perform any moves.
In the second test case, Lee should erase s2.
In the third test case, Lee can make moves, for example, in the following order: 11001101 → 1100101 → 110101 → 10101 → 1101 → 101 → 01.
题目大意
给出一个01组成的字符串,如果字符串中有“10”结构,那么就可以删除这个1和0中的一个数,求怎么删才能让该字符串最短且最小。
题目分析
通过找规律可以我们发现:一个给定的字符串,我们可以删掉该字符串从第一个‘1’出现的位置开始到最后一个‘0’出现的位置为止中间的所有数。
**为了让得到的答案最小,最后剩下的这个‘10’要留下0删掉1。**这样剩下的就是答案了。
例如:11001101->(110011)01[括号中为要删掉的部分]->01(最后剩下的即为答案)
代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=2e5+5;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
string s;
cin>>n;
cin>>s;
int k=-1;
for(int i=0;i<s.size();i++) //找到第一个1出现的位置
if(s[i]=='1')
{
k=i;
break;
}
if(k==-1) {cout<<s<<endl; continue;} //如果字符串中没有1则直接输出原串
int kk=-1;
for(int i=s.size()-1;i>=0;i--) //找到最后一个0出现的位置
if(s[i]=='0')
{
kk=i;
break;
} //如果字符串全为0或者为“0001111”这种结构则直接输出原串
if(kk==-1||k>kk) {cout<<s<<endl; continue;}
string ans=s.substr(0,k); //构建答案串
ans+=s.substr(kk);
cout<<ans<<endl;
}
return 0;
}