题目地址
**题目:**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.
**题意:**字符串s是一个长度为n的二进制字符串(即字符串只包含0-s和1-s)。在一次移动中两个连续的字符si和si+1,如果si是1,si+1是0,删除其中的一个)。删除后字符串收缩。可以进行任意数量的移动(可能为零),
**思路:**不会,百度的代码,百度代码也没怎么看懂。
代码:#include
#include
#include
#include
#include
using namespace std;
const int maxn = 100000 + 15;
int n, a[maxn];
int main()
{
int T;
stack s;
scanf("%d", &T);
while(T --)
{
while(!s.empty()) s.pop();
scanf("%d", &n);
for(int i = 1; i <= n; ++ i) scanf("%1d", &a[i]);
for(int i = n; i; – i)
{
if(s.empty() || s.top() == 1)
{
s.push(a[i]);
continue;
}
if(a[i] == 0) s.push(a[i]);
else
{
while(!s.empty() && !s.top())
{
s.pop();
}
s.push(a[i] - 1);
}
}
while(!s.empty())
{
printf("%d", s.top());
s.pop();
}
puts("");
}
return 0;
}
**总结:**加油学习,追上部队的步伐。