Codeforces Round #392 (Div. 2) B. Blown Garland
题目链接
一开始写了递归来解决,虽然过了,但是赛后看大神的代码,大神思路很简单,
因为题目保证输入是正确的,而且R’, ‘B’, ‘Y’ ,’G’至少有一盏,所以肯定遍历一遍’R’, ‘B’, ‘Y’ ,’G’的最后位置就会有了,注意是最后位置,拿着这个最后位置可以每4个一组的去测试原字符串,肯定是符合的,不符合的输入肯定是错的。
先贴长的递归。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long int ll;
const int maxn = 150;
char s[maxn];
char temp[maxn];
vector<int> v;
int r,b,y,g;
int len;
bool testcan(int plast,char *t,int inde){
bool canR,canB,canY,canG;
canR = canB = canY = canG = true;
// printf("%d ",plast);
if(plast+1<len){
if(t[plast+1]=='R')canR = false;
if(t[plast+1]=='B')canB = false;
if(t[plast+1]=='Y')canY = false;
if(t[plast+1]=='G')canG = false;
}
if(plast+2<len){
if(t[plast+2]=='R')canR = false;
if(t[plast+2]=='B')canB = false;
if(t[plast+2]=='Y')canY = false;
if(t[plast+2]=='G')canG = false;
}
if(plast+3<len){
if(t[plast+3]=='R')canR = false;
if(t[plast+3]=='B')canB = false;
if(t[plast+3]=='Y')canY = false;
if(t[plast+3]=='G')canG = false;
}
if(plast-1>=0){
if(t[plast-1]=='R')canR = false;
if(t[plast-1]=='B')canB = false;
if(t[plast-1]=='Y')canY = false;
if(t[plast-1]=='G')canG = false;
}
if(plast-2>=0){
if(t[plast-2]=='R')canR = false;
if(t[plast-2]=='B')canB = false;
if(t[plast-2]=='Y')canY = false;
if(t[plast-2]=='G')canG = false;
}
if(plast-3>=0){
if(t[plast-3]=='R')canR = false;
if(t[plast-3]=='B')canB = false;
if(t[plast-3]=='Y')canY = false;
if(t[plast-3]=='G')canG = false;
}
if(canR){
// printf("R\n");
t[plast]='R';
r++;
if( inde - 1 < 0 )return true;
if(testcan(v[inde-1],t,inde-1))return true;
else canR = false;
r--;
}
if(canB){
// printf("B\n");
t[plast]='B';
b++;
if( inde - 1 < 0 )return true;
if(testcan(v[inde-1],t,inde-1))return true;
else canB = false;
b--;
}
if(canY){
// printf("Y\n");
t[plast]='Y';
y++;
if( inde - 1 < 0 )return true;
if(testcan(v[inde-1],t,inde-1))return true;
else canY = false;
y--;
}
if(canG){
// printf("G\n");
t[plast]='G';
g++;
if( inde - 1 < 0 )return true;
if(testcan(v[inde-1],t,inde-1))return true;
else canG = false;
g--;
}
if(canR == false && canY==false && canB==false && canG==false){
t[plast] = s[plast];
return false;
}
return true;
}
int main()
{
// freopen("in.txt","r",stdin);
scanf("%s",s);
len = strlen(s);
for(int i=0;i<len;++i)temp[i]=s[i];
temp[len]='\0';
r = b = y = g =0;
for(int i=0;i<len;++i)
if(s[i]=='!')v.push_back(i);
if(!v.empty())testcan(v[v.size()-1],temp,v.size()-1);
else {
for(int i=0;i<len;++i){
if(s[i]=='!'){
if(temp[i]=='R')r++;
if(temp[i]=='B')b++;
if(temp[i]=='Y')y++;
if(temp[i]=='G')g++;
}
}
}
printf("%d %d %d %d\n",r,b,y,g);
return 0;
}
再贴简单的版本,其中ha数组是计数的,a[4]是遍历一遍更新到最后 一定走得通的rygb组合
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long int ll;
const int maxn = 150;
char s[maxn],a[4];
int ha[maxn]={0};
int main()
{
scanf("%s",s);
int len = strlen(s);
for(int i=0;i<len;++i)if(s[i]!='!')a[i%4] = s[i];
for(int i=0;i<len;++i)if(s[i]=='!')ha[ a[i%4] ]++;
printf("%d %d %d %d\n",ha['R'],ha['B'],ha['Y'],ha['G']);
return 0;
}