TopCoder 250 points 4-SRM 145 DIV 2 128.72/250 51.49%

Problem Statement

 

Sometimes when computer programs have a limited number of colors to use, they use a technique called dithering. Dithering is when you use a pattern made up of different colors such that when the colors are viewed together, they appear like another color. For example, you can use a checkerboard pattern of black and white pixels to achieve the illusion of gray.

You are writing a program to determine how much of the screen is covered by a certain dithered color. Given a computer screen where each pixel has a certain color, and a list of all the solid colors that make up the dithered color, return the number of pixels on the screen that are used to make up the dithered color. Each pixel will be represented by a character inscreen. Each character inscreen and in dithered will be an uppercase letter ('A'-'Z') representing a color.

Assume that any pixel which is a color contained in dithered is part of the dithered color.

Definition

 
Class:ImageDithering
Method:count
Parameters:String, String[]
Returns:int
Method signature:int count(String dithered, String[] screen)
(be sure your method is public)
 
 

Constraints

-dithered will contain between 2 and 26 upper case letters ('A'-'Z'), inclusive.
-There will be no repeated characters in dithered.
-screen will have between 1 and 50 elements, inclusive.
-Each element of screen will contain between 1 and 50 upper case letters ('A'-'Z'), inclusive.
-All elements of screen will contain the same number of characters.

Examples

0) 
 
"BW"
{"AAAAAAAA",
 "ABWBWBWA",
 "AWBWBWBA",
 "ABWBWBWA",
 "AWBWBWBA",
 "AAAAAAAA"}
Returns: 24
Here, our dithered color could consist of black (B) and white (W) pixels, composing a shade of gray. In the picture, there is a dithered gray square surrounded by another color (A).
1) 
 
"BW"
{"BBBBBBBB",
 "BBWBWBWB",
 "BWBWBWBB",
 "BBWBWBWB",
 "BWBWBWBB",
 "BBBBBBBB"}
Returns: 48
Here is the same picture, but with the outer color replaced with black pixels. Although in reality, the outer pixels do not form a dithered color, your algorithm should still assume they are part of the dithered pattern.
2) 
 
"ACEGIKMOQSUWY"
{"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX",
 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX"}
Returns: 150
A picture of vertical stripes, every other stripe is considered part of the dithered color.
3) 
 
"CA"
{"BBBBBBB",
 "BBBBBBB",
 "BBBBBBB"}
Returns: 0
The dithered color is not present.
4) 
 
"DCBA"
{"ACBD"}
Returns: 4
The order of the colors doesn't matter.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     

这首题很简单


public class ImageDithering {


	public static int count(String dithered, String[] screen) {
		int num = 0;
		int ditheredArrayLenth = dithered.length();
		int scrLen = 0;
		String scr = null;
		int c[] = new int[26];
		char ch;
		char ditheredArray[] = dithered.toCharArray();
		int len = screen.length;
		for (int i = 0; i < len; i++) {
			scr = screen[i];
			scrLen = scr.length();
			for (int j = 0; j < scrLen; j++) {
				ch = scr.charAt(j);
				c[ch - 65]++;
			}
		}

		for (int i = 0; i < ditheredArrayLenth; i++)
			num += c[ditheredArray[i] - 65];

		
		return num;

	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值