Monday, September 06, 2010

[UVa] 865 - Substitution Cypher

All you need is a table, and because it's such a small limit of possible letters, all you need is a hardly 150-200 sized array in which you can directly map the letters rather than search for them. Helps you access them in 0(1).


#include <stdio.h>
#include <stdlib.h>

char tab[10000];

int main() {

char demo[100], ch, input[1000], plain[200];
int i, t;

scanf("%d",&t);
getchar();
gets(demo);

while (t--) {

for (i=0 ; i<200 ; i++) {
tab[i]=0;
}

gets(plain);

i=0;
while ((ch=getchar())!='\n') {
tab[plain[i++]] = ch;
putchar(ch);
}
putchar('\n');
puts(plain);

while (gets(input)) {
if (!strcmp(input,""))
break;
else {
for (i=0 ; input[i]!='\0' ; i++) {
if (tab[input[i]])
putchar(tab[input[i]]);
else
putchar(input[i]);
}
}
printf("\n");
}

if (t) printf("\n");


}

return 0;

}

No comments:

Post a Comment

Post your comment here. If you want to say something about programming problems, scripts, software etc, please try to be as descriptive as possible.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...