CF1342B Binary Period(数学分析)

简介: CF1342B Binary Period(数学分析)

题目描述



Let's say string ss has period kk if s_i = s_{i + k}si=si+k for all ii from 11 to |s| - k∣s∣−k ( |s|∣s∣ means length of string ss ) and kk is the minimum positive integer with this property.


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


You are given string tt consisting only of 0's and 1's and you need to find such string ss that:


  1. String ss consists only of 0's and 1's;
  2. The length of ss doesn't exceed 2 \cdot |t|2⋅∣t∣ ;
  3. String tt is a subsequence of string ss ;
  4. String ss has smallest possible period among all strings that meet conditions 1—3.


Let us recall that tt is a subsequence of ss if tt can be derived from ss by deleting zero or more elements (any) without changing the order of the remaining elements. For example, tt ="011" is a subsequence of ss ="10101".


输入格式



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


Next TT lines contain test cases — one per line. Each line contains string tt ( 1 \le |t| \le 1001≤∣t∣≤100 ) consisting only of 0's and 1's.


输出格式



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

 

题目描述



假设有一个字符串ss,如果它的第i 个字符和第i +k 个字符相等 (1≤i≤∣s∣−k) ,那么这个字符串的周期为kk 。其中, ∣s∣ 表示字符串 s 的长度。


现在给你一个字符串t ,t  内只包括0和1,请你找出这个 s ,使其满足如下条件:


  1. 字符串 s 也只包括0和1。
  2. ∣s∣≤2×∣t∣(s 的长度不能超过t  的长度的两倍)。
  3. t 是s 的子串。
  4. 在满足上面3个条件的情况下, s 需要有最小的周期 k 。


t 是s  的子串就是说当s  删除0 个或更多个字符后(不能改变顺序), s 就变成了 t 。例如011就是10101的子串。


输入格式



第一行是一个正整数T ,表示数据的组数。


接下来 T行,每行有一个只包含0和1的字符串t (1≤∣t∣≤100)。


输出格式



如果有多种解,输出一个符合条件的字符串 s 即可。


输入输出样例



输入

4

00

01

111

110


输出  

00

01

11111

1010


题意我们分析知道,


我们可以想一下如果我们要满足t是子串,那么我们就要把t里面的0,1都排好比如10(周期最小),如果全部是0,或者1;那么直接输出t;思路就是这样,具体实现看代码。


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
int main() {
  char s[maxn];
    char ans[maxn];
    int T;scanf("%d",&T);
    while(T--) {
        scanf("%s",s+1);
        int n = strlen(s+1);
        int flag = 1;
        for(int i = 2;i <= n;i++) {
            if(s[i] != s[i - 1]) {
                flag = 0;break;
            }
        }
        if(flag) {
            for(int i = 1;i <= n;i++) {
                printf("%c",s[i]);
            }
        }
        else {
            int cnt = 1;
            ans[cnt] = s[1];
            for(int i = 2;i <= n;i++) {
                if(ans[cnt] != s[i]) {
                    ans[++cnt] = s[i];
                }
                else {
                    if(ans[cnt] == '1') {
                        ans[++cnt] = '0';
                    }
                    else if(ans[cnt] == '0') {
                        ans[++cnt] = '1';
                    }
                    ans[++cnt] = s[i];
                }
            }
            for(int i = 1;i <= cnt;i++) {
                printf("%c",ans[i]);
            }
        }
        printf("\n");
    }
}
相关文章
|
关系型数据库 MySQL Linux
CentOS7离线安装MySQL5.7
CentOS7.7离线安装tar包形式的MySQL5.7.33
1576 2
CentOS7离线安装MySQL5.7
|
SQL 流计算 关系型数据库
基于OpenLake的Flink+Paimon+EMR StarRocks流式湖仓分析
阿里云OpenLake解决方案建立在开放可控的OpenLake湖仓之上,提供大数据搜索与AI一体化服务。通过元数据管理平台DLF管理结构化、半结构化和非结构化数据,提供湖仓数据表和文件的安全访问及IO加速,并支持大数据、搜索和AI多引擎对接。本文为您介绍以Flink作为Openlake方案的核心计算引擎,通过流式数据湖仓Paimon(使用DLF 2.0存储)和EMR StarRocks搭建流式湖仓。
1054 5
基于OpenLake的Flink+Paimon+EMR StarRocks流式湖仓分析
|
前端开发 Unix Linux
揭秘 Electron 的 Linux 打包过程:你知道背后发生了什么吗?
本文详细介绍了 `electron-builder` 在 Linux 平台上如何打包 Electron 应用程序,涵盖了 AppImage、Flatpak、Snap 等多种格式的打包原理和具体实现。文章从初始化 `LinuxPackager` 到创建各种目标格式的包,详细解析了每个步骤的代码逻辑和关键方法,帮助开发者更好地理解和使用 `electron-builder` 进行 Linux 应用的打包。
772 2
揭秘 Electron 的 Linux 打包过程:你知道背后发生了什么吗?
|
JavaScript IDE 开发工具
非常规Vue项目配置ESlint和Prettier
这里的非常规指的是,不是使用 `Vue Cli` 这种工具包去创建的项目,或者创建了但是没有加上 `ESlint` 的配置,比如下面这个
|
Unix Linux C语言
【linux】基本指令(中篇)
【linux】基本指令(中篇)
129 1
beamManagement(二)TCI-state/QCL
上一篇讲解了idle初始接入阶段,基站和UE用SSB的索引,关联PRACH的发送时刻比较内涵的指示了波束信息;在RRC建立进入connected mode后,就可以通过TCI State来指示波束信息, 为利于后续内容理解,这里先看下TCI-state及QCL的概念。
|
缓存 监控 网络协议
网工必备网络排错管理工具之IP_MAC地址工具
网工必备网络排错管理工具之IP_MAC地址工具
740 0
网工必备网络排错管理工具之IP_MAC地址工具
|
传感器 机器学习/深度学习 自动驾驶
自动驾驶的商业应用和市场前景
自动驾驶技术已经成为了交通运输领域的一项重要创新。它不仅在改善交通安全性和效率方面具有巨大潜力,还为各种商业应用提供了新的机会。本文将探讨自动驾驶在交通运输中的潜力,自动驾驶汽车的制造商和技术公司,以及自动驾驶的商业模式和市场趋势。
172 0