題目1 : Magic Box【微軟2016校園招聘在線筆試】

時間限制: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 0
RBYRRBY            
4



樣例輸入
1 2 3
RRYBRBRYBRY
樣例輸出
7

import java.util.Scanner;

public class Main {
	
	private static boolean eq(int x,int y,int z,int cr,int cy,int cb){
		int xx = Math.abs(cr-cy);
		int yy = Math.abs(cy-cb);
		int zz = Math.abs(cr-cb);
		if((xx==x&&yy==y&&zz==z)||(xx==x&&yy==z&&zz==y)||
				(xx==y&&yy==z&&zz==x)||(xx==y&&yy==x&&zz==z)||
				(xx==z&&yy==x&&zz==y)||(xx==z&&yy==y&&zz==x)){
			return true;
		}
		return false;
	}
	public static void main(String args[]){
/*		Scanner sc = null;
		try {
			sc = new Scanner(new FileInputStream("D://desktop//test.txt"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int y = sc.nextInt();
		int z = sc.nextInt();
		String str = sc.next();
		int cr = 0,cy =0,cb = 0;
		int res = 0;
		for(int i=0;i<str.length();i++){
			if(str.charAt(i)=='R'){
				cr++;
			}else if(str.charAt(i)=='Y'){
				cy++;
			}else if(str.charAt(i)=='B'){
				cb++;
			}
			if(eq(x,y,z,cr,cy,cb)){
				res = Math.max(res, cr+cy+cb);
				cr = 0;
				cy = 0;
				cb = 0;
			}
		}
		res = Math.max(res, cr+cy+cb);
		System.out.println(res);
		sc.close();
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章