标签:
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
7
/** 题意:给一串有Y B R 组成的字符串,然后如果他们之间的个数差 等与X Y Z 那就消除现在的 字符串,直到字符串结束,看容器中最多有多少个字符 刚开始题意理解错了 他们之间 的随意差值都等于 X Y Z 都行,并且没有重用的;然后就。。。。。 做法:枚举 **/ #include <iostream> #include <string.h> #include <stdio.h> #include <cmath> #include <algorithm> #include <queue> #define maxn 200100 using namespace std; int num[4]; char ch[maxn]; int a,b,c; bool solve() { int ax,ay,bx,by; bool flag1 = false; bool flag2 = false; bool flag3 = false; for(int i=0; i<3; i++) { for(int j=i+1; j<3; j++) { if(abs(num[i] - num[j]) == a) { ax = i; ay = j; flag1 = true; break; } } } for(int i=0; i<3; i++) { for(int j=i+1; j<3; j++) { if(abs(num[i] - num[j]) == b) { if(ax == i && ay == j) continue; bx = i; by = j; flag2 = true; break; } } } for(int i=0; i<3; i++) { for(int j=i+1; j<3; j++) { if(abs(num[i] - num[j]) == c) { if(i == ax &&j == ay) continue; if(i == bx && j == by) continue; flag3 = true; break; } } } if(flag1 && flag2 && flag3) return true; return false; } int main() { //#ifndef ONLINE_JUDGE // freopen("in.txt","r",stdin); //#endif // ONLINE_JUDGE while(~scanf("%d %d %d",&a,&b,&c)) { scanf("%s",ch); memset(num,0,sizeof(num)); int len = strlen(ch); int ans = 0; for(int i=0; i<len; i++) { if(ch[i] == ‘R‘) num[0] ++; else if(ch[i] == ‘B‘) num[1] ++; else num[2]++; ans = max(ans,num[0] + num[1] + num[2]); if(solve()) { // cout<<num[0] <<" "<<num[1] <<" "<<num[2] <<endl; memset(num,0,sizeof(num)); } } printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4556552.html