B - Blown Garland -思维

本文提供了一种解决 CodeForces-758B 问题的方法,该问题涉及分析一串彩色灯泡的排列规律,并确保任何四个连续的灯泡中不会有相同颜色的灯泡出现。通过使用 C++ 编程语言实现了两种不同的算法解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  •  
  • B - Blown Garland

     CodeForces - 758B 
  •  
  • Nothing is eternal in the world
  • if you take any four consecutive light bulbs then there will not be light bulbs with the same color among them. 
  • 仔细分析后发现四个字母为一个循环节。
  • #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        string str;
        map<char,int>mmp;
        char son[5];
        cin>>str;
        for(int i=0; i<str.size(); i++)
            if(str[i]!='!')
                son[(i+1)%4]=str[i];
        for(int i=0; i<str.size(); i++)
            if(str[i]=='!')
                mmp[son[(i+1)%4]]++;
        cout<<mmp['R']<<" "<<mmp['B']<<" "<<mmp['Y']<<" "<<mmp['G']<<endl;
        return 0;
    }

 

#include <bits/stdc++.h>
#define maxn 105
using namespace std;
int a[maxn],n,cnt=0,len;
string str;
char son[maxn];
string to="RBYG";
int temp1,temp2,temp3,temp4;
bool judge(int t)
{
    temp1=temp2=temp3=temp4=0;
    if((t-3)>=0)
    {
        for(int k=t; k>=t-3; k--)
        {
            if(str[k]=='R')
                temp1++;
            else if(str[k]=='B')
                temp2++;
            else if(str[k]=='Y')
                temp3++;
            else if(str[k]=='G')
                temp4++;
        }
        if(temp1>1||temp2>1||temp3>1||temp4>1)
            return 0;
    }
    temp1=temp2=temp3=temp4=0;
    if(t+3<len)
    {
        for(int k=t; k<=t+3; k++)
        {
            if(str[k]=='R')
                temp1++;
            else if(str[k]=='B')
                temp2++;
            else if(str[k]=='Y')
                temp3++;
            else if(str[k]=='G')
                temp4++;
        }
        if(temp1>1||temp2>1||temp3>1||temp4>1)
            return 0;
    }
    temp1=temp2=temp3=temp4=0;
    if(t+2<len&&t-1>=0)
    {
        for(int k=t-1; k<=t+2; k++)
        {
            if(str[k]=='R')
                temp1++;
            else if(str[k]=='B')
                temp2++;
            else if(str[k]=='Y')
                temp3++;
            else if(str[k]=='G')
                temp4++;
        }
        if(temp1>1||temp2>1||temp3>1||temp4>1)
            return 0;
    }
    temp1=temp2=temp3=temp4=0;
    if(t+1<len&&t-2>=0)
    {
        for(int k=t-2; k<=t+1; k++)
        {
            if(str[k]=='R')
                temp1++;
            else if(str[k]=='B')
                temp2++;
            else if(str[k]=='Y')
                temp3++;
            else if(str[k]=='G')
                temp4++;
        }
        if(temp1>1||temp2>1||temp3>1||temp4>1)
            return 0;
    }

    return 1;
}
void dfs(int x,int index)
{
    if(index==cnt)
    {
        temp1=temp2=temp3=temp4=0;
        for(int i=0; i<index; i++)
        {
            if(son[i]=='R')
                temp1++;
            else if(son[i]=='B')
                temp2++;
            else if(son[i]=='Y')
                temp3++;
            else if(son[i]=='G')
                temp4++;
        }
        cout<<temp1<<" "<<temp2<<" "<<temp3<<" "<<temp4<<endl;
        return;
    }
    for(int i=0; i<4; i++)
    {
        str[a[x]]=to[i];
        if(judge(a[x]))
        {
            son[index]=to[i];
            dfs(x+1,index+1);
            str[a[x]]='!';
        }
        else
            str[a[x]]='!';
    }
}
int main()
{
    cin>>str;
    len=str.size();
    for(int i=0; i<len; i++)
        if(str[i]=='!')
            a[cnt++]=i;
    if(cnt>0)
        dfs(0,0);
    else
        cout<<"0 0 0 0"<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值