|
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 wocould become: 123210122 In particle, ifPIs the original string, andQIs 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 (using123210122As an example ):
- AssumeP [0] = 0.
- BecauseQ [0] = P [0] + P [1] = 0 + P [1] = 1, We know thatP [1] = 1.
- BecauseQ [1] = P [0] + P [1] + P [2] = 0 + 1 + P [2] = 2, We know that
P [2] = 1.
- BecauseQ [2] = P [1] + P [2] + P [3] = 1 + 1 + P [3] = 3, We know that
P [3] = 1.
- Repeating these steps gives usP [4] = 0,P [5] = 0,P [6] = 0,P [7] = 1, AndP [8] = 1.
- We check our work by noting thatQ [8] = P [7] + P [8] = 1 + 1 = 2. Since this equation works out, we are finished, and we have recovered one possible original string.
Now we repeat the process, assuming the opposite aboutP [0]:
- AssumeP [0] = 1.
- BecauseQ [0] = P [0] + P [1] = 1 + P [1] = 0, We know thatP [1] = 0.
- BecauseQ [1] = P [0] + P [1] + P [2] = 1 + 0 + P [2] = 2, We know that
P [2] = 1.
- Now note thatQ [2] = P [1] + P [2] + P [3] = 0 + 1 + P [3] = 3, Which leads us to the conclusion thatP [3] = 2. However, this violates the fact that each character in the original string must be '0' or '1'. Therefore, there exists no such original
StringPWhere the first digit is '1 '.
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 stringMessage, Containing the encrypted string, return a vector <string> with exactly two elements. The first element shoshould contain the decrypted string assuming the first character is '0'; the second element shoshould assume First character is '1'. If one of the tests fails, return the string "NONE" in its place. For the above example, you shoshould return{"011100011", "NONE "}. |