Educational Codeforces Round 86 B(构造&模拟)

B. Binary Period

Educational Codeforces Round 86 B(构造&模拟)_第1张图片

Educational Codeforces Round 86 B(构造&模拟)_第2张图片

思路:

题目要求周期长度最小。

  1. 当串已经是000或111串时,直接输出,已经满足条件了。
  2. 当串不满足时。就先输出s【0】,之后跑for,当s【i】==s【i-1】时,前面为0输出1,前面为1输出0,之后就构造出了01串。

反思

这种题目通常都是构造,成功即可。

AC

#include 
#include 
#define For(i,x,y) for(register int i=(x); i<=(y); i++)
using namespace std;
char s[200];
int main()
{
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int cnt1,cnt2;
        cnt1=cnt2=0;
        int len=strlen(s);
        For(i,0,len-1)
        {
            if(s[i]=='1')cnt1++;
            else cnt2++;
        }
        if(cnt1==len||cnt2==len)
        {
            cout<<s<<'\n';
            continue;
        }
        else
        {
            cout<<s[0];
            For(i,1,len-1)
            {
                if(s[i-1]==s[i])
                {
                    if(s[i-1]=='1')cout<<0;
                    else cout<<1;
                }
                cout<<s[i];
            }
            cout<<'\n';
        }
    }
    return 0;
}

你可能感兴趣的:(cf菜鸡的失恋(试炼))