zoukankan      html  css  js  c++  java
  • XidianOJ 1037 倍流畅序列

    题目描述

    对于一个0、1串s, 从左端开始读取它的0获得序列s0,从右端开始读取它的1获得s1,如果s1与s2同构,则称s为倍流畅序列.
    例如:
    011001是一个倍流畅序列, 因为:
    s0 = 0__00_
    s1 = 1__11_

    而101不是, 因为:
    s0 = _0_
    s1 = 1_1

    下面的问题是:对于一个0、1串s, 在s后添加最少数目的0或1,使它成为一个倍流畅序列。

    输入

    有多组输入数据,第一行为一个数字T,代表有T组输入数据 (0<T<=100)。
    接下来为T组数据,每组数据占一行,包含一个长度不超过50的0、1串。

    输出

    一共T行。
    对于每组数据,在一行上输出添加了最少数目的0或1后所得到的倍流畅序列。

    --正文
    字符串的长度为len
    直接遍历当前字符串满不满足,不满足就加一位(最多也就是len个)
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    char str[1000];
    
    int main(){
        int time,T; scanf("%d",&T);
        for (time=1;time<=T;time++){
            scanf("%s",str);
            int now = 0,len = strlen(str);
            int delta = 0;
            bool ok = false;
            while (!ok){
                if (delta == len) break;
                while ( now+delta <= len-1-now) {
                    //printf("%d %d
    ",now+delta,len-1-now);
                    if ((str[now+delta] - '0') ^ (str[len-1-now] - '0') == 1) {
                        now ++;
                    }
                    
                    else {
                        now = 0; delta ++; break;
                    }
                }
                if (now != 0) ok = true; 
            }
            //printf("%d
    ",delta);
            printf("%s",str);
            int i;
            for (i=1;i<=delta;i++){
                char ch = (str[delta-i] - '0') ^ 1;
                //printf("%c %c
    ",str[delta-i],ch+'0');
                printf("%c",ch+'0');
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    騎士宣言
    [洛谷P1631] 序列合并
    [HNOI2006]公路修建问题
    [洛谷2068] 统计和
    [洛谷P1168] 中位数
    【模板】可持久化数组(可持久化线段树/平衡树)
    【模板】可持久化线段树 1(主席树)
    [JSOI2008]最大数maxnumber
    NOI导刊2010提高(06) 黑匣子
    [洛谷1533] 可怜的狗狗
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/6137813.html
Copyright © 2011-2022 走看看