Codeforces A. String Similarity

A binary string is a string where each character is either 0 or 1. Two binary strings a and b of equal length are similar, if they have the same character in some position (there exists an integer i such that ai=bi). For example:

10010 and 01111 are similar (they have the same character in position 4);
10010 and 11111 are similar;
111 and 111 are similar;
0110 and 1001 are not similar.
You are given an integer n and a binary string s consisting of 2n−1 characters. Let’s denote s[l…r] as the contiguous substring of s starting with l-th character and ending with r-th character (in other words, s[l…r]=slsl+1sl+2…sr).

You have to construct a binary string w of length n which is similar to all of the following strings: s[1…n], s[2…n+1], s[3…n+2], …, s[n…2n−1].

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤50).

The second line of each test case contains the binary string s of length 2n−1. Each character si is either 0 or 1.

Output
For each test case, print the corresponding binary string w of length n. If there are multiple such strings — print any of them. It can be shown that at least one string w meeting the constraints always exists.

测试样例

input
4
1
1
3
00000
4
1110000
2
101
output
1
000
1010
00

题意: 定义两个二进制字符串相似的规则为某一位置上字符相同。你会得到一个长度(2n−1)的二进制字符串。现你需构造一个长度为n的二进制字符串w,它相似于以下所有字符串:s[1…n]、s[2…n+1]、s[3…n+2]、…、s[n…2n−1]。

设以上被相似的字符串为 s1,s2,s3…sn。

思路:我们想要构造一个长度为n的字符串w,使其相似于以上全部。则我们可以构造该字符串w,取s1的第一个字符为w的第一个字符。取s2的第二个字符为w的第二个字符…取s3的第n个字符为w的第n个字符。这样就满足了w与他们全部相似的条件。仔细研究发现我们取的字符正好是字符串s的第偶数个字符

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int n,t;
	cin>>t;
	string s;
	while(t--)
	{
		cin>>n;
		cin>>s;
		for(int i=0;i<s.length();i++)
		{
			if(i%2==0)
				cout<<s[i];
		}
		cout<<endl;
	}
}
 

猜你喜欢

转载自blog.csdn.net/henulmh/article/details/108295630
今日推荐