Codeforces Round #640 (Div. 4) F(思维,构造字符串序列)

链接
F. Binary String Reconstruction
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
For some binary string s (i.e. each character si is either ‘0’ or ‘1’), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of ‘1’ (ones) in it was calculated.

You are given three numbers:

n0 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 0;
n1 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 1;
n2 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 2.
For example, for the string s=“1110011110”, the following substrings would be written: “11”, “11”, “10”, “00”, “01”, “11”, “11”, “11”, “10”. Thus, n0=1, n1=3, n2=5.

Your task is to restore any suitable binary string s from the given values n0,n1,n2. It is guaranteed that at least one of the numbers n0,n1,n2 is greater than 0. Also, it is guaranteed that a solution exists.

Input
The first line contains an integer t (1≤t≤1000) — the number of test cases in the input. Then test cases follow.

Each test case consists of one line which contains three integers n0,n1,n2 (0≤n0,n1,n2≤100; n0+n1+n2>0). It is guaranteed that the answer for given n0,n1,n2 exists.

Output
Print t lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.

Example
inputCopy
7
1 3 5
1 1 1
3 9 3
0 1 0
3 1 2
0 0 3
2 0 0
outputCopy
1110011110
0011
0110001100101011
10
0000111
1111
000
题意:给定n0,n1,n2,n0代表子串”00“的个数,n1代表子串”01"与子串“10”个数之和,n2代表子串“11”的个数,要求创造出符合n0,n1,n2个数的字符串,题目绝对有解。0 <= n0,n1,n2 <= 100, n0+n1+n2 >= 1;
思路:先不考虑n1,创建n0+1个0,创建n2+1个1, 先将n0和n2的要求满足,再向中间添加”10“满足n1,还有很多细节需要处理,n0 = 0, n2 = 0,n1奇偶等条件都需要特殊处理。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int T;
	cin >> T;
	while(T--)
	{
		int n0,n1,n2;
		cin >> n0 >> n1 >> n2;
		string s0(n0==0?0:n0+1,'0'),s1,s2(n2==0?0:n2+1,'1');
		if (n0==0&&n2==0){
			int t = 1;
			for(int i = 1 ; i <= n1+1 ; i++)
				t = 1 - t,cout << 1 - t;
		}
		else if ((n0==0)+(n2==0)==1){
			if (n0 == 0){
				for (int i = 1; i <= n1/2; i++) 
					s1 += "10";
				cout << s0 << s1 << s2;
				if (n1 % 2 != 0&&n1 != 0) cout << 0;
			}else{
				for (int i = 1; i <= n1/2; i++)
					s1 += "10";
				cout << s0 << s1 << s2;
				if (n1 % 2 != 0&&n1 != 0) cout << 1;
			}
		}
		else{
			for (int i = 1; i <= (n1-1)/2; i++)
				s1 += "10";
			cout << s0 << s1 << s2;
			if (n1 % 2 == 0&&n1 != 0) cout << 0;
		}
		cout << "\n";
	}
    return 0;
}

codeforces题解:

#include <bits/stdc++.h>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n0, n1, n2;
		cin >> n0 >> n1 >> n2;
		if (n1 == 0) {
			if (n0 != 0) {
				cout << string(n0 + 1, '0') << endl;
			} else {
				cout << string(n2 + 1, '1') << endl;
			}
			continue;
		}
		string ans;
		for (int i = 0; i < n1 + 1; ++i) {
			if (i & 1) ans += "0";
			else ans += "1";
		}
		ans.insert(1, string(n0, '0'));
		ans.insert(0, string(n2, '1'));
		cout << ans << endl;
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值