codeforces B. Binary Period

codeforces B. Binary Period_第1张图片

题目

题意:

给你一个序列 t t t,问一个序列 s s s满足 t t t s s s的子序列且 s s s长度不超过 2 ∗ ∣ t ∣ 2*|t| 2t s s s只由 0 , 1 0,1 0,1组成, s s s的周期要越小越好。

思路:

因为要是子序列,但是不超过 2 ∗ ∣ t ∣ 2*|t| 2t的话,其实我们只要出现 t t t 0 0 0 t t t 1 1 1,那么就都会满足了,但是要最小周期,所以我们就要观察 t t t序列了,如果全是 1 , 0 1,0 1,0的话,那么 s s s序列也全部都是 1 , 0 1,0 1,0,如果是 0 , 1 0,1 0,1均存在的话,那么就直接输出 10101010.... 10101010.... 10101010....

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 110;
char s[maxn];
int main() {
    int t;
    read(t);
    while (t--) {
        scanf("%s", s);
        int len = strlen(s);
        bool flag = false, book = false;
        for (int i = 0; i < len; i++) {
            if (s[i] == '0') flag = true;
            else if (s[i] == '1') book = true;
        }
        if (book && flag) {
            int x = s[0] - '0';
            out(x);
            for (int i = 1; i < 2 * len; i++) {
                out(x ^ 1);
                x ^= 1;
            }
        } else {
            int x = s[0] - '0';
            out(x);
            for (int i = 1; i < 2 * len; i++) {
                out(x);
            }
        }
        putchar('\n');
    }
    return 0;
}

你可能感兴趣的:(codeforces)