Problem Description:
Xiao Ming and Angel are good friends. Recently, their conversations are monitored a detective agency, so they want their conversation is encrypted.
So they invented a new encryption method. Each information is compiled into a binary number B (plain text), the length of which is N.
This information is then write K times rightward 0,1, ..., K-1 bits.
For example: B = 1.00101 million,. 4 K =
1.00101 million
1.00101 million
1.00101 million
1.00101 million
and each column XOR operation, and the final results obtained are recorded, we referred to the number of S (ciphertext).
For example as a result of the above examples: 1110100110.
Finally, transmitting the encoded information S and K to Qi.
Xiao Ming has achieved the encryption process of this encoding, but he asked Angie to write a program to realize this encoding decryption process, can help you achieve Angel decryption process?
Input formats:
The first line of the input two integers N
andK
A second line of the input binary string S
, lengthN + K - 1
Output formats:
Plaintext output B
data range
1<=N<=10^6
1<=K<=10^6
Ideas:
First, given by way of example, the analysis rule compression from the compression law Anti Release decompression algorithm.
Firstly compression process:
0 = 0
1 = 1 ^ 0
1 = 0 ^ 1 ^ 0
0 = 0 ^ 1 ^ 0 ^ 1
0 = 1 ^ 0 ^ 1 ^ 0
1 = 0 ^ 1 ^ 0 ^ 0
0 = 1 ^ 0 ^ 0 ^ 1
1 = 0 ^ 0 ^ 1
1 = 0 ^ 1
1 = 1
The solution requires the plaintext string is set to: ABCDEFG
ciphertext string is set after compression: hijklmnopq
new compression process gives the following analysis:
q = g
p = f ^ g
o = e ^ f ^ g
n = d ^ e ^ f ^g
m = c ^ d ^ e ^ f
l = b ^ c ^ d ^ e
k = a ^ b ^ c ^ d
j = a ^ b ^ c
h = a
The formula a ^ b = c ⇒ a = b ^ c
substitution, inferred:
g = q
f = g ^ p = p ^ q
e = o ^ f ^ g = o ^ p ^ q ^ q = o ^ p
d = n ^ e ^ f ^ g = ... = n ^o
c = m ^ d ^ e ^ f = m ^ n ^ o ^ o ^ p ^ p ^ q = m ^ n ^ q
b = l ^ c ^ d ^ e = l ^ m ^ p
a = k ^ b ^ d ^ d = k ^ l ^ o
The law can be inferred:
0 : res[0] = S[0]
1 ~ K-1 : res[i] = S[i] + S[i-1]
K ~ N res[i] = S[i] + S[i-1] + res[i-k+1]
Complete code:
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N =in.nextInt() , K = in.nextInt();
String S = in.next();
int S1[] = new int[N+K-1];
int res[] = new int[N];
for (int i = 0;i < S.length(); i++) {
S1[i] = S.charAt(i) - '0';
}
res[0] = S1[0];
for (int i = 1;i < K; i++) {
res[i] = S1[i] ^ S1[i-1];
}
for (int i = K;i < N; i++) {
res[i] = S1[i] ^ res[i-K] ^ S1[i-1];
}
for (int i = 0;i < res.length; i++) {
System.out.println(res[i]);
}
}
}