博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
B. Binary Period
阅读量:535 次
发布时间:2019-03-09

本文共 2257 字,大约阅读时间需要 7 分钟。

Let’s say string s has period k if si=si+k for all i from 1 to |s|−k (|s| means length of string s) and k is the minimum positive integer with this property.

Some examples of a period: for s=“0101” the period is k=2, for s=“0000” the period is k=1, for s=“010” the period is k=2, for s=“0011” the period is k=4.

You are given string t consisting only of 0’s and 1’s and you need to find such string s that:

String s consists only of 0’s and 1’s;

The length of s doesn’t exceed 2⋅|t|;
String t is a subsequence of string s;
String s has smallest possible period among all strings that meet conditions 1—3.
Let us recall that t is a subsequence of s if t can be derived from s by deleting zero or more elements (any) without changing the order of the remaining elements. For example, t=“011” is a subsequence of s=“10101”.

Input

The first line contains single integer T (1≤T≤100) — the number of test cases.

Next T lines contain test cases — one per line. Each line contains string t (1≤|t|≤100) consisting only of 0’s and 1’s.

Output

Print one string for each test case — string s you needed to find. If there are multiple solutions print any one of them.

Example

inputCopy
4
00
01
111
110
outputCopy
00
01
11111
1010
Note
In the first and second test cases, s=t since it’s already one of the optimal solutions. Answers have periods equal to 1 and 2, respectively.

In the third test case, there are shorter optimal solutions, but it’s okay since we don’t need to minimize the string s. String s has period equal to 1.

题意

给你一个二进制字符串t,求一个字符串s,满足t是s的子序列,s的长不超过t的两倍,且s的周期最小
思路
因为是二进制字符串,我们让它01或10交替(t的每一个字符变为两个),就可以保证T=2,且s的长度不超过2倍t的长度。
当然,如果字符串t中只有‘1’或者‘0’的话,T=1,我们直接让s=t即可

#include
#include
#include
#include
#include
#include
//#include
using namespace std;typedef long long LL;int main(){ int n; cin >> n; while (n--) { string t; cin >> t; if (t.size() <= 2) //t的大小≤2,显然直接输出即可 { cout << t << endl; } else { int cnt0 = 0,cnt1=0; for (int i = 0; i < t.size(); i++) { if (t[i] == '0') cnt0++; else cnt1++; } if (cnt0 == t.size() || cnt1 == t.size()) { cout << t << endl; goto ca; } char h = t[0], hh=1-(t[0]-'0')+'0'; for (int i = 1; i <= t.size(); i++) { cout << h << hh; } cout << endl; } ca:; }}

转载地址:http://xboiz.baihongyu.com/

你可能感兴趣的文章