The description of the problem:
Give you a sentence which has been decrypted, then give you have 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 [1, 1000] [2, 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.