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

    你可能感兴趣的文章
    Oracle:ORA-00911: 无效字符
    查看>>
    Text-to-Image with Diffusion models的巅峰之作:深入解读 DALL·E 2
    查看>>
    TCP基本入门-简单认识一下什么是TCP
    查看>>
    tableviewcell 中使用autolayout自适应高度
    查看>>
    Orcale表被锁
    查看>>
    svn访问报错500
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>
    SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
    查看>>