Created
August 14, 2012 08:50
-
-
Save iporsut/3347604 to your computer and use it in GitHub Desktop.
ZOJ Problem Set - 1042 W's Cipher
This file contains hidden or 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
#include <stdio.h> | |
#include <string.h> | |
#define MAX 80 | |
char code1[MAX+1]; | |
int pos1[MAX+1]; | |
int code1_i, pos1_i ,len1; | |
char code2[MAX+1]; | |
int pos2[MAX+1]; | |
int code2_i, pos2_i ,len2; | |
char code3[MAX+1]; | |
int pos3[MAX+1]; | |
int code3_i, pos3_i ,len3; | |
int k1, k2, k3; | |
char input[MAX+1]; | |
int i; | |
int main() { | |
scanf("%d %d %d", &k1, &k2, &k3); | |
while(k1 != 0 && k2 != 0 && k3 != 0) { | |
scanf("%s",input); | |
len1 = len2 = len3 = 0; | |
for(i = 0; i < strlen(input); ++i) { | |
if (input[i] >= 'a' && input[i] <= 'i') { | |
code1[len1] = input[i]; | |
pos1[len1] = i; | |
len1++; | |
} else if (input[i] >= 'j' && input[i] <= 'r') { | |
code2[len2] = input[i]; | |
pos2[len2] = i; | |
len2++; | |
} else { | |
code3[len3] = input[i]; | |
pos3[len3] = i; | |
len3++; | |
} | |
} | |
pos1_i = pos2_i = pos3_i = code1_i = code2_i = code3_i = 0; | |
for(i = 0; i < k1; ++i) { | |
code1_i--; | |
if (code1_i < 0) | |
code1_i = len1-1; | |
} | |
for(i = 0; i < k2; ++i) { | |
code2_i--; | |
if (code2_i < 0) | |
code2_i = len2-1; | |
} | |
for(i = 0; i < k3; ++i) { | |
code3_i--; | |
if (code3_i < 0) | |
code3_i = len3-1; | |
} | |
for(i = 0; i < len1; ++i) { | |
input[pos1[pos1_i]] = code1[code1_i]; | |
pos1_i++; | |
code1_i++; | |
if (code1_i == len1) | |
code1_i = 0; | |
} | |
for(i = 0; i < len2; ++i) { | |
input[pos2[pos2_i]] = code2[code2_i]; | |
pos2_i++; | |
code2_i++; | |
if (code2_i == len2) | |
code2_i = 0; | |
} | |
for(i = 0; i < len3; ++i) { | |
input[pos3[pos3_i]] = code3[code3_i]; | |
pos3_i++; | |
code3_i++; | |
if (code3_i == len3) | |
code3_i = 0; | |
} | |
printf("%s\n",input); | |
scanf("%d %d %d", &k1, &k2, &k3); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment