题目
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.
分析
我的理解就是4中颜色的灯,找一个符合他给的序列的循环格式,因为保证每个灯都出现一次,所以直接遍历一边就可以找出4个灯的循环格式,然后遍历第二遍去找!出现的地方应该亮什么灯,统计一下。
ac代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
using namespace std;
//const int maxs = 1e2 + 5;
char c[4] = {'R', 'B', 'Y', 'G'};
int num_col[4];
int ind[4];
string str;
int main(){
cin >> str;
int size = str.size();
for(int i = 0; i < 4; ++i){
for(int j = i; j < size; j += 4){
if(str[j] == '!') {
continue;
}
for(int k = 0; k < 4; ++k){
if(str[j] == c[k]){
ind[i] = k;
}
}
}
}
for(int i = 0; i < 4; ++i){
for(int j = i; j < size; j += 4){
if(str[j] == '!'){
++num_col[ind[i]];
}
}
}
bool t_peace = 1;
for(int i = 0; i < 4; ++i){
if(t_peace){
t_peace = 0;
}
else cout << " ";
cout << num_col[i];
}
return 0;
}