(SRM 144 DIV2) 550分题:
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) | |||||||||||||
|
花了一个小时左右,发现仍然纠结于字符串与数字之间的转换,先研究下提交平台上一个比较好的做法:
String[] s={"",""};
char[] c=message.toCharArray();
int[] q=new int[message.length()];
for(int i=0;i<message.length();i++)
{
q[i]=Integer.parseInt(c[i]+"");
}
以下是我的方法:
String[] result = new String[2];
for(int k=0;k<2;k++){
result[k]="";
}
int length = estring.length();
int[] e = new int[length];//encrypted string
int[] d = new int[length];//decrypted string
for (int i = 0; i < length; i++) {
String temp;
temp = String.valueOf(estring.charAt(i));
e[i] = Integer.valueOf(temp);
}
对比红色部分,我的代码显然繁琐且容易出错。另外数组的初始化也显得比较笨拙。
本题源代码:
public class BinaryCode {
public static String[] decode(String estring) {
String[] result = new String[2];
for(int k=0;k<2;k++){
result[k]="";
}
int length = estring.length();
int[] e = new int[length];//encrypted string
int[] d = new int[length];//decrypted string
for (int i = 0; i < length; i++) {
String temp;
temp = String.valueOf(estring.charAt(i));
e[i] = Integer.valueOf(temp);
}
int m = 0;//for e
int n = 0;//for
for (int k = 0; k < 2; k++) {
d[0] = k;
d[1] = e[0] - d[0];
result[k]+=d[0]+""+d[1]+"";
m = 1;
n = 2;
while (n < length) {
d[n] = e[n - 1] - d[n - 2] - d[n - 1];
if (d[n] < 2) {
result[k] += d[n] + "";
} else {
result[k] = "NONE";
break;
}
n++;
}
}
return result;
}
public static void main(String[] args) {
String a = "12221112222221112221111111112221111";
String[] aresult = new String[a.length()];
aresult = decode(a);
System.out.println(aresult[1]);
}
}