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):
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.
又费了一个多小时
public class BinaryCode {
/**
* @param args
*/
public static void main(String[] args) {
// String s = "123210122";
String s = "12221112222221112221111111112221111";
String arr[] = decode(s);
System.out.print(arr[0] + "," + arr[1]);
}
public static String[] decode(String message) {
boolean flag1 = true;
boolean flag2 = true;
String[] s = new String[2];
int len = message.length();
int s0[] = new int[len + 2];
int s1[] = new int[len + 2];
int t[] = new int[len + 2];
char charArray[] = message.toCharArray();
for (int i = 0; i < len; i++)
t[i + 1] = Integer.parseInt(String.valueOf(charArray[i]));
t[0] = 0;
t[len + 1] = 0;
if (len == 1) {
if (t[1] == 0) {
s[0] = "0";
s[1] = "NONE";
} else if (t[1] == 1) {
s[0] = "NONE";
s[1] = "1";
} else {
s[0] = "NONE";
s[1] = "NONE";
}
return s;
}
{
s0[1] = 0;
s1[1] = 1;
flag1 = getFlag(s0, t, len);
flag2 = getFlag(s1, t, len);
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
if (flag1) {
for (int i = 1; i <= len; i++)
sb1.append(s0[i]);
s[0] = sb1.toString();
} else
s[0] = "NONE";
if (flag2) {
for (int i = 1; i <= len; i++)
sb2.append(s1[i]);
s[1] = sb2.toString();
} else {
s[1] = "NONE";
}
}
return s;
}
private static boolean getFlag(int s[], int t[], int len) {
for (int i = 1; i < len; i++) {
s[i + 1] = t[i] - s[i] - s[i - 1];
if (s[i+1] > 1 || s[i+1] < 0)
return false;
}
if (s[len - 1] + s[len] != t[len])
return false;
return true;
}
}