在这个世界上没有什么是永恒的,Kostya在看到四色花环上死去的花后明白了这个道理。
现在他有一个目标是取代所有死去的花,但他不知道每种颜色的花需要多少朵。保证每种颜色至少有一朵花没有死。
众所周知,花环包含四种颜色的花:红,蓝,黄,绿。花环是这样做的:四个连续的花不会有相同的颜色。举个例子,花环可以长这样 "RYBGRYBGRY", "YBGRYBGRYBG", "BGRYB",但不能长这样 "BGRYG", "YBGRYBYGR" or "BGYBGY"。我们用字符表示花的颜色: 'R' — 红***' — 蓝色, 'Y' — 黄色, 'G' — 绿色.
利用题目所给的信息求出每种花需要多少朵才能修补整个花环。
Input
第一行包含一个字符串 s (4 ≤ |s| ≤ 100),描述整个花环,第 i 个字符描述第 i 朵花的颜色:
- 'R' — 花是红的,
- 'B' — 花是蓝的,
- 'Y' — 花是黄的,
- 'G' — 花是绿的,
- '!' — 花是死的。
保证字符串 s 不包含其他字符。
保证字符串中字符 'R', 'B', 'Y' and 'G'至少出现一次。
保证字符串 s 所描述的花环有解,比如 "GRBY!!!B" 是不可能出现的。
Output
一行,输出4个整数 kr, kb, ky, kg — 分别表示需要加入的红色、蓝色、黄色和绿色的花的数量。
Example
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
第一个样例不需要加入新的花。
第二个样例中,一朵蓝色的花死了。
这个题······脑子当机了QAQ,花了一个半小时才搞出来······
原来想直接暴力的,结果状态太多,不好做,仔细看题才明白,这题的字符串是以4为一周期,利用周期性求解。
还是直接上代码吧:
#include <stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5+10;
const double pi = 3.1415926;
typedef long long ll;
char cnt[10];
int ans[10];
char s[120];
int main()
{
ios::sync_with_stdio(false);
memset(ans, 0, sizeof(ans));
cin >> s;
int n = strlen(s);
for (int i = 0; i < n; i++) {
if (s[i] != '!') {
cnt[(i + 1) % 4] = s[i];//将字母与cnt[1][2][3][4]对应
}
}
for (int i = 0; i < n; i++) {
if (s[i] == '!')
ans[(i + 1) % 4]++;
}
for (int i = 0; i < 5; i++) {
if (cnt[i] == 'R')
{
cout << ans[i]<<' ';
break;
}
}
for (int i = 0; i < 5; i++) {
if (cnt[i] == 'B')
{
cout << ans[i]<<' ';
break;
}
}
for (int i = 0; i < 5; i++) {
if (cnt[i] == 'Y')
{
cout << ans[i]<<' ';
break;
}
}
for (int i = 0; i < 5; i++) {
if (cnt[i] == 'G')
{
cout << ans[i];
break;
}
}
return 0;
}