题意: 有一个01字符串需要清理,对于每个"10"子串,你都可以选择其中一个字符清除。标准规则认为越短的串越干净,同样长的串字典序越小越干净。先让你安装标准规则清理字符串,并输出最干净的结果。
思路:
- 找到左边第一个 ‘1’ 的下标pos1,已经右边第一个 ‘0’ 的下标pos0。
- 若其中没有’0’或没有’1,'或者所有0在1的左边就直接输出原字符串。
- 在整个字符串中部(pos1 到 pos0之间):所有的 ‘1’ 都可被pos0位置的 ‘0’ 清除掉,而所有的 ‘0’ 可以不pos1位置的 ‘1’ 清除掉;所有答案就是原串中 1 ~ pos1 + pos0 ~ n。
- 其实这个题与1375的C. Element Extermination题原理一样的。
代码实现:
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
int t, n;
signed main()
{
IOS;
cin >> t;
while(t --){
cin >> n;
string s, ans; cin >> s;
int pos0 = -1, pos1 = -1;
for(int i = n - 1; ~i; i --){
if(s[i] == '0'){
pos0 = i;
break;
}
}
for(int i = 0; i < n; i ++){
if(s[i] == '1'){
pos1 = i;
break;
}
}
if(pos0 == -1 || pos1 == -1 || pos0 < pos1){
cout << s << endl;
continue;
}
for(int i = 0; i < pos1; i ++)
ans += s[i];
for (int i= pos0; i < s.size(); i ++)
ans += s[i];
cout << ans << endl;
}
return 0;
}