博客
关于我
B. Binary Period
阅读量:538 次
发布时间: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/

你可能感兴趣的文章
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>