Skip to content

Instantly share code, notes, and snippets.

@ayushgoel
Created January 9, 2016 21:30
Show Gist options
  • Save ayushgoel/0e4f742f74ab6978e87a to your computer and use it in GitHub Desktop.
Save ayushgoel/0e4f742f74ab6978e87a to your computer and use it in GitHub Desktop.
//https://www.hackerrank.com/challenges/cipher
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
char xor(char a, char b) {
int ai = a-'0';
int bi = b-'0';
return (ai ^ bi) + '0';
}
int main() {
int n, k;
char s[2000005];
scanf("%d", &n);
scanf("%d", &k);
scanf("%s", s);
char sol[n+1];
sol[0] = s[0];
for (int i = 1; i < k; ++i) {
sol[i] = xor(s[i-1], s[i]);
}
for (int i = k; i < n; ++i) {
sol[i] = xor(xor(s[i-1], sol[i-k]), s[i]);
}
sol[n] = '\0';
printf("%s\n", sol);
return 0;
}
@SGrebenkin
Copy link

Elegant solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment