博客
关于我
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/

你可能感兴趣的文章
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>