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 | |||||||||||||
| |||||||||||||
Limits | |||||||||||||
| |||||||||||||
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.
题意:给出一个加密后的字符串,加密过程为,新字符为原字符加上相信字符的和,求其原来的字符串
代码如下:
class BinaryCode
{
public String[] decode(String message)
{
String[] ans = new String[2];
int n = message.length();
byte[] p = new byte[n];
boolean ok = true;
p[0] = '0';
for (int i = 0; i < n - 1 && ok; i++) {
if (i == 0) {
p[i + 1] = (byte)(message.charAt(i) - (p[i] - '0'));
if (p[i + 1] != '0' && p[i + 1] != '1') ok = false;
} else {
p[i + 1] = (byte)(message.charAt(i) - (p[i - 1] - '0') - (p[i] - '0'));
if (p[i + 1] != '0' && p[i + 1] != '1') ok = false;
}
}
if (n >= 2) {
byte tmp = (byte)(message.charAt(n - 1) - (p[n - 2] - '0'));
if (tmp != p[n - 1]) ok = false;
} else {
char ch = message.charAt(0);
if (ch != '0' && ch != '1') ok = false;
}
if (!ok) {
ans[0] = "NONE";
} else {
String s = new String(p);
ans[0] = s;
}
ok = true;
p[0] = '1';
for (int i = 0; i < n - 1 && ok; i++) {
if (i == 0) {
p[i + 1] = (byte)(message.charAt(i) - (p[i] - '0'));
if (p[i + 1] != '0' && p[i + 1] != '1') ok = false;
} else {
p[i + 1] = (byte)(message.charAt(i) - (p[i - 1] - '0') - (p[i] - '0'));
if (p[i + 1] != '0' && p[i + 1] != '1') ok = false;
}
}
if (n >= 2) {
byte tmp = (byte)(message.charAt(n - 1) - (p[n - 2] - '0'));
if (tmp != p[n - 1]) ok = false;
} else {
char ch = message.charAt(0);
if (ch != '0' && ch != '1') ok = false;
}
if (!ok) {
ans[1] = "NONE";
} else {
String s = new String(p);
ans[1] = s;
}
return ans;
}
}