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

你可能感兴趣的文章
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>