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

    你可能感兴趣的文章
    Python yield generator
    查看>>
    Python yield 使用浅析
    查看>>
    Python yield解析:深入理解生成器的魔力
    查看>>
    Python You are using pip version 10.0.1, however version 19.3.1 is available.
    查看>>
    python zipfile模块学习笔记(一)
    查看>>
    Python zip函数 详解(全)
    查看>>
    Python \r\n与\n的转换
    查看>>
    python __new__中单例的作用
    查看>>
    python | aiofiles,一个超酷的 Python 库!
    查看>>
    python | akshare,一个超强的 开源Python 金融数据接口库!
    查看>>
    python | alabaster,一个强大的 关于alabaster 主题 Python 库!
    查看>>
    python | algorithms,一个超赞的 集合常用算法的Python 库!
    查看>>
    python调用jpype 报错:OSError JVM is already started和JVM cannot be restarted
    查看>>
    python | authlib,一个强大的 Python 库!
    查看>>
    python | awswrangler,一个高效的 Python 库!
    查看>>
    python | bashplotlib,一个有趣的Python库!
    查看>>
    python | bentoml,一个超级厉害的 模型部署 Python 库!
    查看>>
    python调用jar包的模块_python调用jar包
    查看>>
    python | black,一个神奇的 代码格式化工具 Python 库!
    查看>>
    python | bleach,一个超强的 Python 库!
    查看>>