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\n",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\n",delta);
        printf("%s",str);
        int i;
        for (i=1;i<=delta;i++){
            char ch = (str[delta-i] - '0') ^ 1;
            //printf("%c %c\n",str[delta-i],ch+'0');
            printf("%c",ch+'0');
        }
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/ToTOrz/p/6137813.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值