hihocoder 1135 : Magic Box

10619 단어 code
#1135 : Magic Box
시간 제한:
10000ms
단일 시한:
1000ms
메모리 제한:
256MB
묘사
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 0RBYRRBY            
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
",ans); } return 0; }

좋은 웹페이지 즐겨찾기