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;
}

embedcpp-2021-03是一个有关嵌入式C++编程的课程,于2021年3月举办。嵌入式C++编程是指在嵌入式系统中使用C++编程语言进行开发的一种方法。 在嵌入式系统中,资源通常是有限的,例如处理器速度、内存容量和存储空间等。因此,使用C++编程语言可以提供更高的灵活性和效率,帮助开发人员充分利用有限的资源。C++在嵌入式系统中的应用范围广泛,例如物联网设备、汽车电子和工业自动化等领域。 embedcpp-2021-03课程旨在向学员介绍嵌入式C++编程的基础知识和技巧。课程内容通常包括以下方面: 1. C++语法和特性:介绍C++的基本语法、面向对象编程和泛型编程等概念,以及C++11、C++14和C++17的一些新特性。 2. 嵌入式系统概述:了解嵌入式系统的基本特点、硬件和软件组成,以及与传统桌面开发的区别。 3. 低级编程:学习如何与硬件交互,包括使用寄存器、配置外设和处理中断等。还可以介绍使用汇编语言优化性能的技巧。 4. 内存管理:探讨嵌入式系统中的内存管理技术,包括堆栈和堆的使用、动态内存分配和对象生命周期管理等。 5. 实时操作系统(RTOS):介绍嵌入式系统中常用的实时操作系统,如FreeRTOS和µC/OS等,学习如何使用RTOS进行任务调度和资源管理。 除了理论知识,embedcpp-2021-03课程通常还包括实际的项目练习,以帮助学员将所学知识应用于实际场景。通过该课程,学员可以了解嵌入式C++编程的基础概念和实践技巧,为嵌入式系统开发提供了一定的基础。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值