标签:
刚开始学习C语言,准备在做hiho的题目的过程中来学习,在此进行记录,如果代码中有错误或者不当的地方还请指正。
The circus clown Sunny has a magic box. When the circus is performing, Sunny puts some balls into the box one by one. The balls are in three colors: red(R), yellow(Y) and blue(B). Let Cr, Cy, Cb denote the numbers of red, yellow, blue balls in the box. Whenever the differences among Cr, Cy, Cb happen to be x, y, z, all balls in the box vanish. Given x, y, z and the sequence in which Sunny put the balls, you are to find what is the maximum number of balls in the box ever.
For example, let‘s assume x=1, y=2, z=3 and the sequence is RRYBRBRYBRY. After Sunny puts the first 7 balls, RRYBRBR, into the box, Cr, Cy, Cb are 4, 1, 2 respectively. The differences are exactly 1, 2, 3. (|Cr-Cy|=3, |Cy-Cb|=1, |Cb-Cr|=2) Then all the 7 balls vanish. Finally there are 4 balls in the box, after Sunny puts the remaining balls. So the box contains 7 balls at most, after Sunny puts the first 7 balls and before they vanish.
Line 1: x y z
Line 2: the sequence consisting of only three characters ‘R‘, ‘Y‘ and ‘B‘.
For 30% data, the length of the sequence is no more than 200.
For 100% data, the length of the sequence is no more than 20,000, 0 <= x, y, z <= 20.
The maximum number of balls in the box ever.
Another Sample
Sample Input | Sample Output |
0 0 0 RBYRRBY |
4 |
1 2 3 RRYBRBRYBRY
#include<stdio.h> #include<stdlib.h> #define SWAP(a,b) tmp=a;a=b;b=tmp #define SORT(a,b,c,tmp) if(a>b) \ {SWAP(a,b);} if(b>c) {SWAP(b,c);} if(a>b) {SWAP(a,b);} #define ABS(a) (a)<0?(-a):(a) #define MAX(a,b) a>b?a:b int main() { int x,y,z; int Dc1,Dc2,Dc3; int CrN=0,CyN=0,CbN=0; int MaxNum=0,Num=0; int i,j,tmp; char Color; scanf("%d%d%d",&x,&y,&z); SORT(x,y,z,tmp); while((Color=getchar())!=EOF) { switch (Color) { case ‘R‘: CrN++; Num++; break; case ‘Y‘: CyN++; Num++; break; case ‘B‘: CbN++; Num++; break; case ‘\n‘: break; default: exit(EXIT_FAILURE); break; } Dc1=CrN-CyN; Dc1=ABS(Dc1); Dc2=CrN-CbN; Dc2=ABS(Dc2); Dc3=CyN-CbN; Dc3=ABS(Dc3); SORT(Dc1,Dc2,Dc3,tmp); if(x==Dc1&&y==Dc2&&z==Dc3) { CrN=0; CyN=0; CbN=0; MaxNum=MAX(Num,MaxNum); Num=0; } } MaxNum=MAX(Num,MaxNum); printf("%d\n",MaxNum); return 0; }
标签:
原文地址:http://www.cnblogs.com/ZH-0/p/5001929.html