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

    你可能感兴趣的文章
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm设置镜像如淘宝:http://npm.taobao.org/
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>
    NPM酷库052:sax,按流解析XML
    查看>>
    npm错误 gyp错误 vs版本不对 msvs_version不兼容
    查看>>
    npm错误Error: Cannot find module ‘postcss-loader‘
    查看>>
    npm,yarn,cnpm 的区别
    查看>>
    NPOI
    查看>>
    NPOI之Excel——合并单元格、设置样式、输入公式
    查看>>
    NPOI初级教程
    查看>>
    NPOI利用多任务模式分批写入多个Excel
    查看>>
    NPOI在Excel中插入图片
    查看>>
    NPOI将某个程序段耗时插入Excel
    查看>>
    NPOI格式设置
    查看>>