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

你可能感兴趣的文章
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>