I fixed this bug, but still WA.
My code:
Code: Select all
#include <stdio.h>
#include <string.h>
#define TARGET "the quick brown fox jumps over the lazy dog"
#define TARGETLEN 43
#define MAX_CHARS 100
#define MAX_LINES 100
#define CODETABLE_SIZE 26
#define CODETABLE_BASE (int)'a'
int checkline(char *, char *);
void decrypt(int, char *, char *);
int main()
{
char lines[MAX_LINES][MAX_CHARS], codetable[CODETABLE_SIZE], *line;
int i, nlines, ncases, decodable;
scanf("%d", &ncases);
getchar();
getchar();
for(i = 0; i < ncases; i++){
memset(lines, 0, sizeof(char) * MAX_CHARS * MAX_LINES);
nlines = 0;
line = (char *)lines;
decodable = 0;
while(fgets(line, sizeof(char) * MAX_CHARS, stdin) && strlen(line) > 1){
if(decodable == 0 && strlen(line) - 1 == TARGETLEN && checkline(line, codetable))
decodable = 1;
nlines++;
line = line + MAX_CHARS;
}
if(decodable == 1){
decrypt(nlines, (char *)lines, codetable);
}else{
printf("No solution.\n");
}
if(i < ncases - 1)
printf("\n");
}
return 0;
}
int checkline(char *line, char *codetable)
{
int i;
char *target = TARGET;
size_t len = strlen(line) - 1;
int codeindex;
memset(codetable, 0, sizeof(char) * CODETABLE_SIZE);
for(i = 0; i < len; i++){
codeindex = (int)line[i] - CODETABLE_BASE;
if(line[i] == ' '){
if(target[i] != ' ')
break;
}else if(target[i] == ' '){
break;
}else if(codetable[codeindex] == '\0'){
if(strchr(codetable, target[i])){
break;
}else{
codetable[codeindex] = target[i];
}
}else if(codetable[codeindex] != target[i]){
break;
}
}
if(i == len)
return 1;
else
return 0;
}
void decrypt(int nlines, char *lines, char *codetable)
{
int i, j;
char *line;
line = lines;
for(i = 0; i < nlines; i++){
for(j = 0; j < strlen(line) - 1; j++)
if(line[j] != ' ')
line[j] = codetable[(int)line[j] - CODETABLE_BASE];
printf("%s", line);
line = line + MAX_CHARS;
}
}
My input
Code: Select all
3
vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
awerwaerwrwa
vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
awerwaerwrwa
vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
awerwaerwrwa
vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
awerwaerwrwa
vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
awerwaerwrwa
My output
Code: Select all
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
xzurzxurzrzx
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
xzurzxurzrzx
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
xzurzxurzrzx
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
xzurzxurzrzx
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
xzurzxurzrzx