• B. Blown Garland


    http://codeforces.com/problemset/problem/758/B

    B. Blown Garland
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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.

     思路:因为每个字都会至少给出一个,我们知道a[i] == a[i+4],a[i] == a[i-4],如果不是话就不能构成连续4

    个都不同。那么直接模拟添加。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 char str[3000];
     5 map<char,int>my;
     6 int flag[5];
     7 int cnt[5];
     8 int main(void)
     9 {
    10     scanf("%s",str);
    11     int l = strlen(str);
    12     char c;
    13     c = 'R';
    14     my[c] = 1;
    15     c = 'B';
    16     my[c] = 2;
    17     c = 'Y';
    18     my[c] = 3;
    19     c = 'G';
    20     my[c] = 4;
    21     memset(cnt,0,sizeof(cnt));
    22     while(true)
    23     {   int c = 0;
    24        for(int i = 0;i < l;i++)
    25        {
    26           if(str[i] != '!')
    27           {
    28               if(i-4 >= 0&&str[i-4]=='!')
    29               {
    30                   str[i-4] =  str[i];
    31                   cnt[my[str[i]]]++;
    32                   c = 1;
    33               }
    34               if(i+4 < l&&str[i+4]=='!')
    35               {
    36                   str[i+4] = str[i];
    37                   cnt[my[str[i]]]++;
    38                   c = 1;
    39               }
    40           }
    41        }
    42        if(!c)break;
    43     }int sum = 0;
    44     for(int i = 0;i < l;i++)
    45     {
    46         if(str[i] == '!')
    47         {
    48             sum++;
    49         }
    50     }
    51     for(int i = 1;i <= 4;i++)
    52         cnt[i]+=sum/4;
    53     //puts(str);
    54     printf("%d",cnt[1]);
    55     for(int i = 2; i <= 4; i++)
    56         printf(" %d",cnt[i]);
    57     return 0;
    58 }
  • 相关阅读:
    第11条:谨慎地覆盖clone
    第10条:始终要覆盖toString
    第9条:覆盖equals时总是覆盖hashCode
    第8条:覆盖equals时请遵守通用约定
    第7条:避免使用终结方法
    第6条:消除过期的对象引用
    第5条:避免创建不必要的对象
    第4条:通过私有构造器来强化不可实例化能力
    第3条:用私有构造器或者枚举类型强化Singleton属性
    第2条:遇到多个构造器参数时要考虑用构建器
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/6329680.html
Copyright © 2020-2023  润新知