zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    java IO流详解
    java设计模式之单例模式(几种写法及比较)
    JS定时刷新页面及跳转页面
    遍历map的四种方法
    String 中去掉空格
    TSP问题_遗传算法(STL大量使用)
    无向图的深度优先生成树和广度优先生成树
    Prim算法求最小生成树
    哈夫曼编码_静态库
    中序线索化二叉树
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/6329680.html
Copyright © 2011-2022 走看看