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

    你可能感兴趣的文章
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node-RED中使用JSON数据建立web网站
    查看>>
    Node-RED中使用json节点解析JSON数据
    查看>>
    Node-RED中使用node-random节点来实现随机数在折线图中显示
    查看>>
    Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
    查看>>