博客
关于我
B. Binary Period
阅读量:538 次
发布时间:2019-03-09

本文共 1116 字,大约阅读时间需要 3 分钟。

为了解决这个问题,我们需要找到一个字符串 s,使其满足以下条件:

  • s 是二进制字符串。
  • s 的长度不超过 2 * |t|
  • ts 的一个子序列。
  • s 的周期尽可能小。
  • 方法思路

    为了构造满足条件的字符串 s,我们可以分以下几步进行:

  • 检查特殊情况:如果 t 中所有字符都是相同的(全0或全1),则 s 就直接等于 t,周期为1。
  • 构造周期为2的字符串:如果 t 不是全0或全1,那么我们构造一个新的字符串 s,其中每个字符重复两次,并按原顺序排列。这样,s 的周期为2,且 ts 的子序列。
  • 这种方法确保了 s 的长度不超过 2 * |t|,并且 s 的周期尽可能小。

    解决代码

    #include 
    using namespace std;string constructS(const string &t) { bool all_zero = true; bool all_one = true; for (char c : t) { if (c == '0') { all_one = false; } else { all_zero = false; } } if (all_zero || all_one) { return t; } string s; for (char c : t) { s += c; s += c; } return s;}int main() { int T; cin >> T; for (int _ = 0; _ < T; ++_) { string t; cin >> t; if (t.size() <= 2) { cout << t << endl; } else { string s = constructS(t); cout << s << endl; } }}

    代码解释

  • 读取输入:读取测试用例的数量 T 和每个测试用例的字符串 t
  • 处理特殊情况:检查 t 是否全0或全1。如果是,直接输出 t
  • 构造字符串 s:如果 t 不是全0或全1,构造一个新的字符串 s,其中每个字符重复两次,并按原顺序排列。
  • 输出结果:输出构造的字符串 s
  • 这种方法确保了 s 的周期尽可能小,同时满足所有给定的条件。

    转载地址:http://xboiz.baihongyu.com/

    你可能感兴趣的文章
    Node.js的交互式解释器(REPL)
    查看>>
    Node.js的循环与异步问题
    查看>>
    Node.js高级编程:用Javascript构建可伸缩应用(1)1.1 介绍和安装-安装Node
    查看>>
    nodejs + socket.io 同时使用http 和 https
    查看>>
    NodeJS @kubernetes/client-node连接到kubernetes集群的方法
    查看>>
    NodeJS API简介
    查看>>
    Nodejs express 获取url参数,post参数的三种方式
    查看>>
    nodejs http小爬虫
    查看>>
    nodejs libararies
    查看>>
    nodejs npm常用命令
    查看>>
    nodejs npm常用命令
    查看>>
    Nodejs process.nextTick() 使用详解
    查看>>
    NodeJS yarn 或 npm如何切换淘宝或国外镜像源
    查看>>
    nodejs 中间件理解
    查看>>
    nodejs 创建HTTP服务器详解
    查看>>
    nodejs 发起 GET 请求示例和 POST 请求示例
    查看>>
    NodeJS 导入导出模块的方法( 代码演示 )
    查看>>
    nodejs 开发websocket 笔记
    查看>>
    nodejs 的 Buffer 详解
    查看>>
    NodeJS 的环境变量: 开发环境vs生产环境
    查看>>