Created
January 9, 2016 21:30
-
-
Save ayushgoel/0e4f742f74ab6978e87a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Elegant solution