2020-05-18 — 习题训练一D

VJ

D - Binary Period

题意:根据给定的t子序列,输出周期k最小的字符串s.

解题思路:分为两种情况,只含有一种字符的字符串与同时含有1,0的字符串。

     若要使周期尽可能小,则使循环尽可能多。

ac代码:

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	int t,m,i,a;
	char s[105];
	char s1[5]="10";
	cin>>t;
	while(t--){
		cin>>s;
		m=strlen(s);
		if(m<=2) cout<<s<<endl;
		else{
			a=0;
			for(i=0;i<m-1;i++){
				if(s[i]!=s[i+1]){
					a=1;//判断字符串是否只含“0”字符或“1”字符
break; } } if(a==0){ cout<<s<<endl;//只含有一种字符直接输出 } else{ for(i=0;i<m;i++){ cout<<s1;//循环输出“10”字符串即可
} cout<<endl; } } } return 0; }

 
原文地址:https://www.cnblogs.com/nanan/p/12922808.html