题目描述
对于一个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; }