The description of the problem:
give you a sentence which has been decrypted, then give you many lines of cryptograph,and there may be one sentence matched with the decrypted sentence. if you founded , you must decrypt all the lines and out put them . or else, print No solution.
The algorithm:
There are three steps to judge which line is mathched. 1.the lenghth of the sentence.2.the location of the space.3.whether having 26 letters and a letter only match one letter. If the three conditions is meeted, I can get the mapped array of the 26 letters,then I can decrypted all the lines.
The source code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str[1000][200];
char ch[50]={"the quick brown fox jumps over the lazy dog"};
char encode[27];
int num;
int matchall(int t)
{
int j;
for(j=0;j<26;j++)
encode[j]='0';
for(j=0;j<43;j++)
{
if(ch[j]!=' ')
{
if(encode[str[t][j]-'a']=='0')
{
encode[str[t][j]-'a']=ch[j];
}
else if(encode[str[t][j]-'a']!=ch[j])
{
return 0;
}
}
}
for(j=0;j<26;j++)
{
if(encode[j]=='0')
return 0;
}
return 1;
}
int judge()
{
int i,len;
for(i=0;i<num;i++)
{
len=strlen(str[i]);
if(len==43)
{
if(str[i][3]==' '&&str[i][9]==' '&&str[i][15]==' '&&str[i][19]==' '
&&str[i][25]==' '&&str[i][30]==' '&&str[i][34]==' '&&str[i][39]==' ')
{
if(matchall(i))
return 1;
}
}
}
return 0;
}
void print()
{
int i,j,len;
for(i=0;i<num;i++)
{
len=strlen(str[i]);
for(j=0;j<len;j++)
{
if(str[i][j]!=' ')
str[i][j]=encode[str[i][j]-'a'];
}
}
for(i=0;i<num;i++)
printf("%s/n",str[i]);
}
int main()
{
/*
freopen(".in","r",stdin);
freopen(".out","w",stdout);
*/
int i,t;
scanf("%d",&t);
getchar();
getchar();
while(t--)
{
i=0;
memset(str,0,sizeof(str));
while(1)
{
gets(str[i]);
if(strcmp(str[i],"")==0)
break;
i++;
}
num=i;
if(judge())
{
print();
}
else
printf("No solution./n");
if(t>=1)
printf("/n");
}
return 0;
}
Sum up:
I got a WA,because I judged the blank line by "gets()!=NULL".But, I do not understand why it is wrong, so I changed the judgement to "strcmp(str,"")==0",then AC.Someone explain it , please.