Problem Statement |
|
Let's say you have a binary string such as the following: 011100011 One-of-the-encrypt this string is-to-add-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 are the encrypted string, thenq[i] = p[i-1] + p[i] + p[i+1] for all digit positions I. Characters off the left and right edges of the string is treated as zeroes. An encrypted string given to the this format can is decoded as follows (using123210122 as an example):
- Assume p[0] = 0.
- Because q[0] = p[0] + p[1] = 0 + p[1] = 1, we know that p[1] = 1.
- Because q[1] = p[0] + p[1] + p[2] = 0 + 1 + p[2] = 2, we know that p[2] = 1.
- Because q[2] = p[1] + p[2] + p[3] = 1 + 1 + p[3] = 3, we know that p[3] = 1.
- Repeating these steps gives us p[4] = 0, p[5] = 0, p[6] = 0,p[7] = 1, and P [8] = 1.
- We Check our work by noting this q[8] = p[7] + p[8] = 1 + 1 = 2. Since This equation works out, we is finished, and we have recovered one possible original string.
Now we repeat the process, assuming the opposite about P[0]:
- Assume p[0] = 1.
- Because q[0] = p[0] + p[1] = 1 + p[1] = 1, we know that p[1] = 0.
- Because q[1] = p[0] + p[1] + p[2] = 1 + 0 + p[2] = 2, we know that p[2] = 1.
- Now note this q[2] = p[1] + p[2] + p[3] = 0 + 1 + p[3] = 3, which leads us to the conclusion, p[3] = 2. However, this violates the fact, each character in the original string must is ' 0 ' or ' 1 '. Therefore, there exists no such original stringP where the first digit is ' 1 '.
Note that this algorithm produces at the most of the decodings for any given encrypted string. There can never be more than one possible-to-decode a string once the first binary digit is set. Given A string message, containing the encrypted string, return a string[] with exactly, 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 it 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 was public) |
|
Limits |
|
Time limit (s): |
2.000 |
Memory Limit (MB): |
64 |
|
Constraints |
- |
message would contain between 1 and characters, inclusive. |
- |
Each character in message would be either ' 0 ', ' 1 ', ' 2 ', or ' 3 '. |
Examples |
0) |
|
|
|
Returns: {"011100011", "NONE"} |
|
|
1) |
|
|
|
Returns: {"%", "10"} |
We know that one of the digits must is ' 1 ', and the other must is ' 0 '. We return both cases. |
|
|
2) |
|
|
|
Returns: {"NONE", "11001"} |
Since the first digit of the encrypted string is ' 2 ', the first and digits of the original string must be ' 1 '. Our test fails if we try to assume thatp[0] = 0. |
|
|
3) |
|
|
|
Returns: {"None", "None"} |
The same as the first example, but the rightmost digit have been changed to something inconsistent with the rest of The original string. No Solutions is possible. |
|
|
4) |
|
|
|
Returns: {"None", "None"} |
|
|
5) |
|
|
"12221112222221112221111111112221111" |
|
Returns: {"01101001101101001101001001001101001", "10110010110110010110010010010110010"} |
|
|
This problem statement are 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 p Rohibited. (c) 2003, TopCoder, Inc. All rights reserved.
The number of decrypted strings is calculated, because it is 01 strings, so there can be up to two of them.
The second time using Java to solve problems, will it be like a Java shell in the C + + program?
Practical experience:
C + + to Java is really not difficult, the biggest difficulty is to know how to use some Java functions, such as the string processing, if the use of C + + nature is directly add or use the direct push_back VC, but Java seems to have a what StringBuilder class , here I directly + = connected.
So the problem of C + + to Java is actually a memory problem, there is no understanding problem, because Java has some concepts, C + + almost all, understand the barrier is not.
Finally, we are familiar with a slightly controversial conclusion: as long as you are familiar with a language, then learning other languages will be very easy.
Personally, I support this conclusion. The premise is to know the "familiarity" of the two words of the component.
Read the two C + + classic books said that they are not familiar with C + + is wrong, like the introduction of the algorithm to say that they know the algorithm is not correct, need a lot of practice, thinking, deep experience.
public class Binarycode {private Boolean equ (int a, int b) {return a = = B;} Public string[] Decode (string ms) {string rs[] = new String[2];if (Ms.isempty ()) return rs;rs[0] = GetCode (MS, "0"); rs[1] = GetCode (MS, "1");//check the end bitint n = rs[0].length (); int a = Rs[0].charat (n-1)-' 0 '; int b = n > 1? Rs[0].charat (n-2)-' 0 ': 0;if (A + B + ' 0 '! = Ms.charat (Ms.length ()-1)) rs[0] = "NONE"; n = rs[1].length (); a = Rs[1].chara T (n-1)-' 0 '; b = n > 1? Rs[1].charat (n-2)-' 0 ': 0;if (A + B + ' 0 '! = Ms.charat (Ms.length ()-1)) rs[1] = "NONE"; return RS; String GetCode (String ms, String dec) {int n = ms.length (), if (equ (1, N)) return dec;dec + = string.valueof (Ms.charat (0)-D Ec.charat (0));//each time the next char is computed for the I subscript, it is only necessary to loop to n-1 for the for (int i = 1; i < n-1; i++) {int a = Dec.charat (i-1)-' 0 '; int b = Dec.charat (i)-' 0 '; int c = Ms.charat (i)-' 0 '; int d = c-a-b;if (!equ (0, D) &&!equ (1, D)) return "NONE";d EC + = string.valueof (d);} return Dec;}}
public class Main {public static void main (string[] args) {binarycode BC = new Binarycode (); string[] str = bc.decode ("123210122"); System.out.print (str[0] + ' \ n ' + str[1]);}}
TopCoder srms 1 String processing problem Java troubleshooting