美文网首页
Codeforces 1342B - Binary Period

Codeforces 1342B - Binary Period

作者: 费城的二鹏 | 来源:发表于2020-05-07 09:56 被阅读0次

    COCO奶茶第一次喝,好多人的,貌似很出名的样子,招牌很可爱,味道也不错[贪吃]。

    第二次通过,第一次是因为翻译错误了,我的翻译呀,头疼。ps:这也是我刷题的目的之一。

    题目: Codeforces 1342B - Binary Period

    https://codeforces.com/problemset/problem/1342/B

    翻译

    题目是二进制周期。

    定义字符串 s 的周期 k,s(i)=s(i+k) {i 是 从 1 到 len(s) - 1},并且 k 是所有符合条件中最小的正整数。

    给你一个字符串 t,仅包含 0 和 1,需要找到满足以下要求的字符串 s:

    1. s 仅包含 0 和 1;
    2. 字符串的长度不超过 2 * len(t);
    3. 字符串 t 是 s 的子字符串;
    4. 要求找到满足条件 1-3 的最短周期的字符串 s。

    输入格式

    第一行输入 T。

    接下来 T 行,每行输入字符串 t,仅包含 0 和 1。

    输出格式

    每行输出 s,如果有多个答案,输出任何一个答案。

    分析

    这道题跟 1348B 非常相似。

    https://codeforces.com/problemset/problem/1348/B

    都是要构造循环字符串。

    需要构造出满足要求的最短循环字符串。因为只有两种字符,所以循环节点只有 1 或者 2,分成两种情况。

    1. 如果 t 只有 1 或者 0,那么直接输出原字符串 t。
    2. 如果 t 既有 1 和 0,那么最短的循环周期就是2,根据字符串长度输出相应数量的 01 就可以了。

    以上就是全部分析。

    代码(Python3)

    通过记录
    # https://codeforces.com/problemset/problem/1342/B
    
    import sys
    
    # sys.stdin = open(r"./file/input.txt", 'r')
    # sys.stdout = open(r"./file/output.txt", 'w')
    
    t = int(input())
    
    for _ in range(t):
        str = input()
    
        result = ""
        index = 0
        if '0' in str and '1' in str:
            for c in str:
                result += "01"
        else:
            result = str
        print(result)
    

    更多代码尽在 https://github.com/Tconan99/Codeforces

    by 费城的二鹏 2020.05.06 长春

    相关文章

      网友评论

          本文标题:Codeforces 1342B - Binary Period

          本文链接:https://www.haomeiwen.com/subject/wnvgghtx.html