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");
    }
}
相关文章
|
算法
专题九Simulink仿真基础-1
专题九Simulink仿真基础
885 1
|
NoSQL 测试技术 Linux
VLDB顶会论文Async-fork解读与Redis在得物的实践(2)
VLDB顶会论文Async-fork解读与Redis在得物的实践
607 0
VLDB顶会论文Async-fork解读与Redis在得物的实践(2)
|
6月前
|
人工智能 自然语言处理 安全
2026企业级Agent解决方案,主流agent平台评测与选型指南
本文聚焦2026年企业级AI Agent规模化落地关键期,提出开发效率、安全合规、生态集成三大选型标尺,深度剖析主流架构。阿里云瓴羊Agent凭借NL2Data混合技术、全链路智能能力及深厚企业底座,成为数据密集型行业首选,助力企业实现“懂业务、守底线、通全局”的智能升级。(239字)
|
6月前
|
存储 人工智能 NoSQL
知识库构建:将采集到的数据存入向量数据库,打造企业私域知识库
本文手把手教你用OpenClaw构建企业AI知识库:解决PDF难检索、AI不懂业务、数据用完即弃等痛点。详解Builtin(开箱即用)、LanceDB(永久记忆)和阿里云Tablestore/Hologres(团队协作)三套向量数据库方案,含配置模板、命令及避坑指南,全程本地化、数据私有。(239字)
609 0
|
6月前
|
人工智能 监控 安全
AI智能体APP的开发
本指南详解AI智能体APP从0到1的五阶段落地路径:聚焦“感知-推理-行动”闭环,涵盖人设定义、知识库构建、RAG增强、智能编排、安全过滤及数据反哺,助力开发者高效打造可控、可信、可进化的AI原生应用。(239字)
|
7月前
|
编解码 网络安全
如何利用Aeroshell的VNC连接功能 ?基于Deepin桌面环境配置远程桌面完整指南
本文详解Deepin DDE下TigerVNC远程桌面黑屏/断连问题的根因与解决方案:绕过systemd依赖,手动启动dde-session、dock等核心组件,并配置dbus环境;附tigervnc 共享桌面备选方案及防火墙、自启配置,实测可用。(239字)
545 0
|
运维 监控 关系型数据库
MySQL高可用方案:MHA与Galera Cluster对比
本文深入对比了MySQL高可用方案MHA与Galera Cluster的架构原理及适用场景。MHA适用于读写分离、集中写入的场景,具备高效写性能与简单运维优势;而Galera Cluster提供强一致性与多主写入能力,适合对数据一致性要求严格的业务。通过架构对比、性能分析及运维复杂度评估,帮助读者根据自身业务需求选择最合适的高可用方案。
|
11月前
|
人工智能 算法 机器人
职场新规则:比谁加班多已经过时了,现在比的是谁更会当“AI调度官”
本文揭示AI Agent时代职场新规则:努力≠成功,“AI调度官”正以智能体工作流(Agentic Workflow)重构竞争力——用逻辑调度算力,以系统替代苦干。告别内卷加班,拥抱“睡后生产力”,成为驾驭AI的流程建筑师与人性翻译官。
490 1
|
存储 人工智能 算法
清晰价值始于关系的建立
本文提出“关系理性”范式,重构人工智能从“工具理性”向“伙伴型AI”转变的理论与路径。通过情感共鸣、边界感知与协同进化三维模型,结合语义记忆、动态规则与反馈闭环技术,推动AI从功能服务转向有温度的人机关系构建。
872 0