2021-03-03

题目链接 https://codeforces.com/problemset/problem/1369/B
B. AccurateLee
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
5
10
0001111111
4
0101
8
11001101
10
1110000000
1
1
outputCopy
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,输出压缩后,在所有最短的字串中选择字典序最小的字符串输出。
过程模拟(这个是看的别人的)
10
0001111111

0001111111

位置12345678910
数值0001111111
自左往右查找,第一个1出现的位置l=4,
自右往左查找,第一个0出现的位置r=3,
不符合l<r,故字串无压缩,直接输出0001111111

4
0101

001

位置1234
数值0101
自左往右查找,第一个1出现的位置l=2,
自右往左查找,第一个0出现的位置r=3,
符合l<r,故区间[2,3]中的字串10可压缩为0,故字串压缩后,输出001

8
11001101

01

位置12345678
数值11001101

自左往右查找,第一个1出现的位置l=1,
自右往左查找,第一个0出现的位置r=7,
符合l<r,故区间[1,7]中的字串1100110可压缩为0,故字串压缩后,输出01

10
1110000000

0

位置12345678910
数值1110000000

自左往右查找,第一个1出现的位置l=1,
自右往左查找,第一个0出现的位置r=10,
符合l<r,故区间[1,10]中的字串1110000000可压缩为0,故字串压缩后,输出0

1
1

1

位置1
数值1

自左往右查找,第一个1出现的位置l=1,
自右往左查找,第一个0出现的位置r,找不到,
不符合l<r,故字串无压缩,直接输出1

下面和代码中注意c++中substr函数应用
#include
#include
using namespace std;
int main()
{
  string s(“12345asdf”);
  string a = s.substr(0,5);
//获得字符串s中从第0位开始的长度为5的字符串
  cout << a << endl;
}
输出如下:
12345

注意
1 用途:一种构造string的方法
2 形式:s.substr(pos, n)
3 解释:返回一个string,
包含s中从pos开始的n个字符的拷贝
(pos的默认值是0,n的默认值是s.size() - pos,
即不加参数会默认拷贝整个s)

代码
#include
#include
#include // substr函数(第几位开始,长度) [闭区间]

using namespace std;

int main()
{
int t,n;
string s;
cin >> t;
while (t–) {
cin >> n;
string s;
cin >> s;
int i = 0, j = n;
while (i < n && s[i] == ‘0’)
i++; //注意此时i指针指在左第一个1的位置,与substr()参数有关
// substr合并的是0到i的上一个位置这段区间
//这段长度是i而非i-1
while (j > 0 && s[j - 1] == ‘1’)
j–;
if (i==j)
cout<< s << endl;
else //左第一个1到右第一个0的区间(闭)合并成数字0,其余不变
cout << s.substr(0, i) + ‘0’ + s.substr(j) << endl;
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值