Codeforces Round #392 (Div. 2) B. Blown Garland(规律)

B. Blown Garland
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Nothing is eternal in the world, Kostya understood it on the 7-th of January when he saw partially dead four-color garland.

Now he has a goal to replace dead light bulbs, however he doesn’t know how many light bulbs for each color are required. It is guaranteed that for each of four colors at least one light is working.

It is known that the garland contains light bulbs of four colors: red, blue, yellow and green. The garland is made as follows: if you take any four consecutive light bulbs then there will not be light bulbs with the same color among them. For example, the garland can look like “RYBGRYBGRY”, “YBGRYBGRYBG”, “BGRYB”, but can not look like “BGRYG”, “YBGRYBYGR” or “BGYBGY”. Letters denote colors: ‘R’ — red, ‘B’ — blue, ‘Y’ — yellow, ‘G’ — green.

Using the information that for each color at least one light bulb still works count the number of dead light bulbs of each four colors.

Input
The first and the only line contains the string s (4 ≤ |s| ≤ 100), which describes the garland, the i-th symbol of which describes the color of the i-th light bulb in the order from the beginning of garland:

‘R’ — the light bulb is red,
‘B’ — the light bulb is blue,
‘Y’ — the light bulb is yellow,
‘G’ — the light bulb is green,
‘!’ — the light bulb is dead.
The string s can not contain other symbols except those five which were described.

It is guaranteed that in the given string at least once there is each of four letters ‘R’, ‘B’, ‘Y’ and ‘G’.

It is guaranteed that the string s is correct garland with some blown light bulbs, it means that for example the line “GRBY!!!B” can not be in the input data.

Output
In the only line print four integers kr, kb, ky, kg — the number of dead light bulbs of red, blue, yellow and green colors accordingly.

Examples
input
RYBGRYBGR
output
0 0 0 0
input
!RGYB
output
0 1 0 0
input
!!!!YGRB
output
1 1 1 1
input
!GB!RG!Y!
output
2 1 1 0
Note
In the first example there are no dead light bulbs.

In the second example it is obvious that one blue bulb is blown, because it could not be light bulbs of other colors on its place according to the statements.

题意:给出一个字符串,!代表未知,RBYG代表四种颜色,补全!,使得任意连续四个灯的颜色均不相同。

思路:假设当前是第i栈灯,那么它的颜色一定和i+4*j或者i-4*j的灯颜色相同,其中j取1开始的正整数。
对于遇到的任意一个!只需要往前或者往后找不是!的字符即可确定当前!的颜色。

代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
const int maxn=105;
char str[maxn];
int main()
{
    scanf("%s",str+1);
    int len=strlen(str+1);
    int num_1=0,num_2=0,num_3=0,num_4=0;
    for(int i=1;i<=len;i++)
    {
        if(str[i]=='!')
        {
            for(int j=1;i+j*4<=len||i-j*4>0;j++)
            {
                if(i+j*4<=len&&str[i+j*4]!='!')
                {
                    str[i]=str[i+j*4];//这算不算效率优化?
                    if(str[i]=='R')
                        num_1++;
                    else if(str[i]=='B')
                        num_2++;
                    else if(str[i]=='Y')
                        num_3++;
                    else if(str[i]=='G')
                        num_4++;
                    break;
                }
                else if(i-j*4>0&&str[i-j*4]!='!')
                {
                    str[i]=str[i-j*4];
                    if(str[i]=='R')
                        num_1++;
                    else if(str[i]=='B')
                        num_2++;
                    else if(str[i]=='Y')
                        num_3++;
                    else if(str[i]=='G')
                        num_4++;
                    break;
                }
            }
        }
    }
    printf("%d %d %d %d\n",num_1,num_2,num_3,num_4);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值