#652 (Div. 2)B. AccurateLee(找規律)

題目描述

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章