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, then Q[i] = P[i-1] + P[i] + P[i+1] for all digit positions i. 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 (using 123210122 as an example):
Now we repeat the process, assuming the opposite about P[0] :
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
| |||||||||||||
|
| ||||||||||||
|
| ||||||||||||
|
| ||||||||||||
Constraints
| |||||||||||||
-
|
message
will contain between 1 and 50 characters, inclusive.
| ||||||||||||
-
|
Each character in message will be either '0', '1', '2', or '3'.
| ||||||||||||
Examples
| |||||||||||||
0)
|
| ||||||||||||
|
| ||||||||||||
1)
|
| ||||||||||||
|
| ||||||||||||
2)
|
| ||||||||||||
|
| ||||||||||||
3)
|
| ||||||||||||
|
| ||||||||||||
4)
|
| ||||||||||||
|
| ||||||||||||
5)
|
| ||||||||||||
|
|
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.
Code:
BinaryCode.java
package org.topcoder;
import java.io.IOException;
/**
* Given a encrypt String message and return the possible orginal string
* @author ahpo.yang
* @author TCSDeveloper
* @version 1.0
*/
public class BinaryCode {
/**
* <p>Constructors,initially</p>
*/
public BinaryCode() {
* Given a encrypt String message and return the possible orginal string
* @author ahpo.yang
* @author TCSDeveloper
* @version 1.0
*/
public class BinaryCode {
/**
* <p>Constructors,initially</p>
*/
public BinaryCode() {
}
/**
* Decode the input message to the orginal
* @param message
* @return String[]
*/
public String[] decode(String message) {
// Define the return String[]
String[] retStr = {"",""};
// Process check flg
boolean blnFlag = false;
// The encrypt message length
int intMsgLength;
int[] retArray;
try {
/* Message check start */
// Null check
if (null == message) {
System.out.println("Message is Null!");
System.exit(0);
}
intMsgLength = message.length();
// Message length check
if (intMsgLength > 50) {
System.out.println("Message length limit is 50!");
System.exit(0);
}
// Message character check
for (int i = 0; i < message.length(); i++) {
if (message.charAt(i) > '3' || message.charAt(i) < '0') {
System.out.println("The " + String.valueOf(i)
+ " character of message is not in [0,1,2,3]!");
System.exit(0);
}
}
/* Message check end */
retArray = new int[intMsgLength];
/* Message decode start */
for (int m = 0; m < 2; m++) {
// Assum the first character is m
retArray[0] = m;
if (intMsgLength > 1) {
// Check the input message length
retArray[1] = Integer.parseInt(message.substring(0, 1))
- retArray[0];
for (int i = 2; i < intMsgLength; i++) {
// Process Failed
if (retArray[1] > 1) break;
String strTemp = "";
// Decode the encrypted character of the message
strTemp = String.valueOf(message.charAt(i - 1));
retArray[i] = Integer.parseInt(strTemp) - retArray[i - 1] - retArray[i - 2];
}
} else {
// Process Check flg
blnFlag = true;
}
// Check the Process result
if (blnFlag) {
// Message have only one character
if (Integer.parseInt(message) == retArray[0]) {
retStr[m] = String.valueOf(retArray[0]);
} else {
retStr[m] = "NONE";
}
} else {
// Message have more than one character
if (Integer.parseInt(String.valueOf(message
.charAt(intMsgLength - 1))) == (retArray[intMsgLength - 1] + retArray[intMsgLength - 2])) {
// Process Success
for (int i = 0; i < retArray.length; i++) {
// Consturct the original message
retStr[m] += retArray[i];
}
} else {
// Process Failed
retStr[m] = "NONE";
}
}
}
/* Message decode end */
} catch (Exception e) {
e.printStackTrace();
} finally {
// Program Clean;
retArray = null;
}
// Return the result;
return retStr;
}
}
* Decode the input message to the orginal
* @param message
* @return String[]
*/
public String[] decode(String message) {
// Define the return String[]
String[] retStr = {"",""};
// Process check flg
boolean blnFlag = false;
// The encrypt message length
int intMsgLength;
int[] retArray;
try {
/* Message check start */
// Null check
if (null == message) {
System.out.println("Message is Null!");
System.exit(0);
}
intMsgLength = message.length();
// Message length check
if (intMsgLength > 50) {
System.out.println("Message length limit is 50!");
System.exit(0);
}
// Message character check
for (int i = 0; i < message.length(); i++) {
if (message.charAt(i) > '3' || message.charAt(i) < '0') {
System.out.println("The " + String.valueOf(i)
+ " character of message is not in [0,1,2,3]!");
System.exit(0);
}
}
/* Message check end */
retArray = new int[intMsgLength];
/* Message decode start */
for (int m = 0; m < 2; m++) {
// Assum the first character is m
retArray[0] = m;
if (intMsgLength > 1) {
// Check the input message length
retArray[1] = Integer.parseInt(message.substring(0, 1))
- retArray[0];
for (int i = 2; i < intMsgLength; i++) {
// Process Failed
if (retArray[1] > 1) break;
String strTemp = "";
// Decode the encrypted character of the message
strTemp = String.valueOf(message.charAt(i - 1));
retArray[i] = Integer.parseInt(strTemp) - retArray[i - 1] - retArray[i - 2];
}
} else {
// Process Check flg
blnFlag = true;
}
// Check the Process result
if (blnFlag) {
// Message have only one character
if (Integer.parseInt(message) == retArray[0]) {
retStr[m] = String.valueOf(retArray[0]);
} else {
retStr[m] = "NONE";
}
} else {
// Message have more than one character
if (Integer.parseInt(String.valueOf(message
.charAt(intMsgLength - 1))) == (retArray[intMsgLength - 1] + retArray[intMsgLength - 2])) {
// Process Success
for (int i = 0; i < retArray.length; i++) {
// Consturct the original message
retStr[m] += retArray[i];
}
} else {
// Process Failed
retStr[m] = "NONE";
}
}
}
/* Message decode end */
} catch (Exception e) {
e.printStackTrace();
} finally {
// Program Clean;
retArray = null;
}
// Return the result;
return retStr;
}
}
BinaryCodeTest.java
package org.topcoder;
import junit.framework.TestCase;
public class BinaryCodeTest extends TestCase {
public void testDecode() {
BinaryCode bc= new BinaryCode();
String[] retAssum={"011100011","NONE"};
String[] retResult=new String[2];
String testMessage="";
testMessage="123210122";
retAssum[0]="011100011";
retAssum[1]="NONE";
retResult=bc.decode(testMessage);
assertEquals("First 1: ",retAssum[0],retResult[0]);
assertEquals("First 2: ",retAssum[1],retResult[1]);
testMessage="11";
retAssum[0]="01";
retAssum[1]="10";
retResult=bc.decode(testMessage);
assertEquals("Second 1: ",retAssum[0],retResult[0]);
assertEquals("Second 2: ",retAssum[1],retResult[1]);
retAssum[0]="01";
retAssum[1]="10";
retResult=bc.decode(testMessage);
assertEquals("Second 1: ",retAssum[0],retResult[0]);
assertEquals("Second 2: ",retAssum[1],retResult[1]);
testMessage="22111";
retAssum[0]="NONE";
retAssum[1]="11001";
retResult=bc.decode(testMessage);
assertEquals("Third 1: ",retAssum[0],retResult[0]);
assertEquals("Third 2: ",retAssum[1],retResult[1]);
testMessage="123210120";
retAssum[0]="NONE";
retAssum[1]="NONE";
retResult=bc.decode(testMessage);
assertEquals("Forth 1: ",retAssum[0],retResult[0]);
assertEquals("Forth 2: ",retAssum[1],retResult[1]);
testMessage="3";
retAssum[0]="NONE";
retAssum[1]="NONE";
retResult=bc.decode(testMessage);
assertEquals("Fifth 1: ",retAssum[0],retResult[0]);
assertEquals("Fifth 2: ",retAssum[1],retResult[1]);
testMessage="12221112222221112221111111112221111";
retAssum[0]="01101001101101001101001001001101001";
retAssum[1]="10110010110110010110010010010110010";
retResult=bc.decode(testMessage);
assertEquals("Sixth 1: ",retAssum[0],retResult[0]);
assertEquals("Sixth 2: ",retAssum[1],retResult[1]);
}
retAssum[0]="NONE";
retAssum[1]="11001";
retResult=bc.decode(testMessage);
assertEquals("Third 1: ",retAssum[0],retResult[0]);
assertEquals("Third 2: ",retAssum[1],retResult[1]);
testMessage="123210120";
retAssum[0]="NONE";
retAssum[1]="NONE";
retResult=bc.decode(testMessage);
assertEquals("Forth 1: ",retAssum[0],retResult[0]);
assertEquals("Forth 2: ",retAssum[1],retResult[1]);
testMessage="3";
retAssum[0]="NONE";
retAssum[1]="NONE";
retResult=bc.decode(testMessage);
assertEquals("Fifth 1: ",retAssum[0],retResult[0]);
assertEquals("Fifth 2: ",retAssum[1],retResult[1]);
testMessage="12221112222221112221111111112221111";
retAssum[0]="01101001101101001101001001001101001";
retAssum[1]="10110010110110010110010010010110010";
retResult=bc.decode(testMessage);
assertEquals("Sixth 1: ",retAssum[0],retResult[0]);
assertEquals("Sixth 2: ",retAssum[1],retResult[1]);
}
}