分享TopCoder的一道題目和個人答案

Problem Statement

 

Let's say you have a binary string such as the following:

011100011

One way to encrypt this string is to add to each digit the sum of its adjacent digits. For example, the above string would become:

123210122

In particular, if P is the original string, and Q is the encrypted string, thenQ[i] = P[i-1] + P[i] + P[i+1] for all digit positionsi. Characters off the left and right edges of the string are treated as zeroes.

An encrypted string given to you in this format can be decoded as follows (using123210122 as an example):

  1. Assume P[0] = 0.
  2. Because Q[0] = P[0] + P[1] = 0 + P[1] = 1, we know that P[1] = 1.
  3. Because Q[1] = P[0] + P[1] + P[2] = 0 + 1 + P[2] = 2, we know that P[2] = 1.
  4. Because Q[2] = P[1] + P[2] + P[3] = 1 + 1 + P[3] = 3, we know that P[3] = 1.
  5. Repeating these steps gives us P[4] = 0, P[5] = 0, P[6] = 0,P[7] = 1, andP[8] = 1.
  6. We check our work by noting that Q[8] = P[7] + P[8] = 1 + 1 = 2. Since this equation works out, we are finished, and we have recovered one possible original string.

Now we repeat the process, assuming the opposite about P[0]:

  1. Assume P[0] = 1.
  2. Because Q[0] = P[0] + P[1] = 1 + P[1] = 1, we know that P[1] = 0.
  3. Because Q[1] = P[0] + P[1] + P[2] = 1 + 0 + P[2] = 2, we know that P[2] = 1.
  4. Now note that Q[2] = P[1] + P[2] + P[3] = 0 + 1 + P[3] = 3, which leads us to the conclusion thatP[3] = 2. However, this violates the fact that each character in the original string must be '0' or '1'. Therefore, there exists no such original stringP where the first digit is '1'.

Note that this algorithm produces at most two decodings for any given encrypted string. There can never be more than one possible way to decode a string once the first binary digit is set.

Given a String message, containing the encrypted string, return a String[] with exactly two elements. The first element should contain the decrypted string assuming the first character is '0'; the second element should assume the first character is '1'. If one of the tests fails, return the string "NONE" in its place. For the above example, you should return{"011100011", "NONE"}.

Definition

 
Class: BinaryCode
Method: decode
Parameters: String
Returns: String[]
Method signature: String[] decode(String message)
(be sure your method is public)

Limits

 
Time limit (s): 2.000
Memory limit (MB): 64

Constraints

- message will contain between 1 and 50 characters, inclusive.
- Each character in message will be either '0', '1', '2', or '3'.

Examples

0)  
 
"123210122"
Returns: { "011100011",  "NONE" }

The example from above.

1)  
 
"11"
Returns: { "01",  "10" }

We know that one of the digits must be '1', and the other must be '0'. We return both cases.

2)  
 
"22111"
Returns: { "NONE",  "11001" }

Since the first digit of the encrypted string is '2', the first two digits of the original string must be '1'. Our test fails when we try to assume thatP[0] = 0.

3)  
 
"123210120"
Returns: { "NONE",  "NONE" }

This is the same as the first example, but the rightmost digit has been changed to something inconsistent with the rest of the original string. No solutions are possible.

4)  
 
"3"
Returns: { "NONE",  "NONE" }
 
5)  
 
"12221112222221112221111111112221111"
Returns: 
{ "01101001101101001101001001001101001",
  "10110010110110010110010010010110010" }
 

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.     






以下是個人答案:

class BinaryCode
{
BinaryCode(){}

public String[] decode(String hello)
{
//char a ;

String p0 = "0";
int signFor0  = 0;
String p1 = "1";
int signFor1 = 0;//0代表及格

if(hello.length() == 1)
{
int tem0 = (int)(hello.charAt(0)-48)-(int)(p0.charAt(0)-48);
// System.out.println("first:"+tem0);
if(tem0 != 0 && tem0 != 1){
// System.out.println("fail0:"+tem0);
signFor0 = 1;} //1 代表不及格
p0 = p0.concat(tem0+"");

int tem1 = (int)(hello.charAt(0)-48)-(int)(p1.charAt(0)-48);
// System.out.println("first:"+tem1);
if(tem1 != 0 && tem1 != 1){
// System.out.println("fail1:"+tem1);
signFor1 = 1;} //1 代表不及格
p1 = p1.concat(tem1+"");
}

for(int i=0;i<hello.length()-1;i++)
{
//a = hello.charAt(i);
if(i==0)
{
 
int tem0 = (int)(hello.charAt(i)-48)-(int)(p0.charAt(i)-48);
System.out.println("first:"+tem0);
if(tem0 != 0 && tem0 != 1){System.out.println("fail0:"+tem0);signFor0 = 1;} //1 代表不及格
p0 = p0.concat(tem0+"");

int tem1 = (int)(hello.charAt(i)-48)-(int)(p1.charAt(i)-48);
System.out.println("first:"+tem1);
if(tem1 != 0 && tem1 != 1){System.out.println("fail1:"+tem1);signFor1 = 1;} //1 代表不及格
p1 = p1.concat(tem1+"");

 
}
else
{
int temp0 = (int)(hello.charAt(i)-48)-(int)(p0.charAt(i)-48)-(int)(p0.charAt(i-1)-48);
// System.out.println("hello.charAt(i):  "+hello.charAt(i));
// System.out.println("p0.charAt(i):" +p0.charAt(i));
// System.out.println("p0.charAt(i-1): "+p0.charAt(i-1));
if(temp0 != 0 && temp0 != 1){System.out.println("fail0:"+temp0);signFor0 = 1;} //1 代表不及格

p0 = p0.concat(temp0+"");


int temp1 = (int)(hello.charAt(i)-48)-(int)(p1.charAt(i)-48)-(int)(p1.charAt(i-1)-48);
if(temp1 != 0 && temp1 != 1){System.out.println("fail1:"+temp1);signFor1 = 1;} //1 代表不及格

p1 = p1.concat(temp1+"");


}
}
//
// System.out.println(p0);
// System.out.println(p1);

if(signFor0 == 1){p0 = "NONE";}
if(signFor1 == 1){p1 = "NONE";}

String[] abc = new String[]{p0,p1};

return abc;
}
}



得分是 94+(滿分300),額,明天去看看300分的牛人的答案。



以下是300分答案:

class BinaryCode
{
BinaryCode(){}

public String[] decode(String hello)
{
String zeroFirst = "0";
String oneFirst = "1";

for(int i=0;i< hello.length()-1;i++)
{
int digit = Character.getNumericValue(hello.charAt(i));
zeroFirst = updateString(zeroFirst,digit,i);
oneFirst  = updateString(oneFirst,digit,i);
}

return new String[]{checkNumber(hello,zeroFirst),checkNumber(hello,oneFirst)};

}

public String updateString(String input, int digit, int index)
{
if(!input.equals("NONE"))
{
int toadd = digit - Character.getNumericValue(input.charAt(index));

// System.out.println("input.length(): "+input.length());

if(input.length()>1)
{
// System.out.println("input.length()"+input.length());
toadd -= Character.getNumericValue(input.charAt(index-1));
}

if(toadd == 0 || toadd == 1){
// System.out.println("input.concat");
input += toadd ;
// System.out.println(input);
}
else{
input =  "NONE";
}
}


return input;
}

public String checkNumber(String origin , String encoded)
{

int originL_1 = Character.getNumericValue(origin.charAt(origin.length()-1));
int encoded_1 = Character.getNumericValue(encoded.charAt(encoded.length()-1));
if(origin.length()>1)
{
if( (originL_1 - encoded_1) != Character.getNumericValue(encoded.charAt(encoded.length()-2)))  encoded =  "NONE";
}
else if(originL_1 != encoded_1)  //在這裏將只有一個的情況搞掂了,比如說 STRING origin = "3";
{
encoded = "NONE";
}
return encoded;
}
}



收穫: 利用 sysout來找bug是個好習慣

什麼都看不進時,拿着一本書去樓下讀,也能入腦。接下來,設計QQ界面和設計數據庫結構。




500pointSrm513Div2

/*Problem Statement
    
You may remember an old computer game called "The Incredible Machine". It was a game where you could simulate simple processes like balls falling, lasers shooting, or cats pursuing mice. Manao is faced with the following problem in this game.  The game is 2-dimensional. To make solving the problem easier, Manao introduced the cartesian coordinates on the screen. The OX axis goes from left to right and coincides with the ground. The OY axis goes from bottom to top.  There are N horizontal platforms mounted at different heights. The length of the i-th platform is platformLength[i] and it is mounted at point (platformMount[i], i + 1). Each platform can be moved horizontally in such a way that it does not disconnect from its mount, i.e., the mount resides between its ends or on one of them. In other words, the leftmost possible position of the i-th platform is when its left end is at (platformMount[i] - platformLength[i], i + 1) and the rightmost position is when its right end is at (platformMount[i] + platformLength[i], i + 1). The platforms may only be moved by integer distances, so both left and right ends of a platform are always located at points with integer coordinates.  Several balls will simultaneously fall downwards to the ground from a height that is above all platforms. All balls will fall vertically and the i-th of them will fall at X-coordinate balls[i]. The balls are very small and can be considered as points. Manao should set the platforms' placement in such a way that no ball falls on a platform. Falling on an end of a platform counts as falling on a platform. Manao is not allowed to move the platforms once the balls start falling.  Count the number of ways to place the platforms so that all of the balls miss them. Return this number modulo 1,000,000,009. Two placements are different if there's a platform that has different positions in these placements.
Definition
    
Class:
YetAnotherIncredibleMachine
Method:
countWays
Parameters:
int[], int[], int[]
Returns:
int
Method signature:
int countWays(int[] platformMount, int[] platformLength, int[] balls)
(be sure your method is public)
Limits
    
Time limit (s):
2.000
Memory limit (MB):
64
Constraints
-
platformMount will contain between 1 and 50 elements, inclusive.
-
Each element of platformMount will be between -10000 and 10000, inclusive.
-
platformLength will contain the same number of elements as platformMount.
-
Each element of platformLength will be between 1 and 10000, inclusive.
-
balls will contain between 1 and 50 elements, inclusive.
-
Each element of balls will be between -10000 and 10000, inclusive.
-
All elements of balls will be distinct.
Examples
0)


    
{7}
{10}
{3,4}
Returns: 3
A platform of length 10 is mounted at point (7, 1). Two balls will fall at coordinates 3 and 4. There are three placements of the platform which let the ball miss it: setting the platform's left end at X-coordinate 5, 6 and 7.
1)


    
{1,4}
{3,3}
{2,7}
Returns: 1
The only placement which ensures that balls land aside the platforms is when platform 0's right end is at point (1, 1) and platform 1's left end is at (3, 2).
2)


    
{4,4,4}
{10,9,8}
{1,100}
Returns: 27
There are 3 possible placements for each of the platforms.
3)


    
{0}
{1}
{0}
Returns: 0
There is no way to move the platform away from the ball's trajectory.
4)


    
{100, -4215, 251}
{400, 10000, 2121}
{5000, 2270, 8512, 6122}
Returns: 250379170


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.*/



import java.util.*;

public class YetAnotherIncredibleMachine
{
	final static int MOD = 1000000009;
	final static int N = 50000;

	public static void main(String[] args){
		YetAnotherIncredibleMachine temp = new YetAnotherIncredibleMachine();

		int[] platformMount = {4, 4, 4};
		int[] platformLength = {10, 9, 8};
		int[] balls = {1, 100 };
 
		System.out.println(temp.countWays499(platformMount,platformLength,balls));
	}

	public int countWays499(int[] platformMount, int[] platformLength, int[] balls)
	{
		for(int i =0 ; i< platformMount.length;i++){
			platformMount[i] += N/2;
			 
		}
		for(int i =0 ; i<balls.length;i++){
			balls[i] += N/2;
			 
		}
		Arrays.sort(balls);//排序
		int[] sum = new int[N];
		for(int i=1;i<N;i++){
			sum[i] = sum[i-1];
			if(Arrays.binarySearch(balls,i)>=0)
				++sum[i];
			 // find 1 - 50000 in balls , if find add sum[i]
		}
		long res = 1;
		for(int i=0;i<platformLength.length;i++){
			int L = platformMount[i] - platformLength[i];
			int R = platformMount[i];
			int cur = 0;
			for(int x=L;x<=R;x++){
				if(sum[x+platformLength[i]] - sum[x-1]==0){
					++cur;
				}
			}
			res = res*cur%MOD;
		}
		return (int)res;
	}
}



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