Question: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 799
The question is to decrypt the ciphertext. The encryption process is based on a key word. Assume that the keyword is "batboy" and the plaintext is "meet me by the old oak tree ". Because the keywords consist of 6 letters, we write the plain text (ignore spaces and punctuation) into 6 columns (add some random letters at the end ):
Meetme
Bytheo
Ldoach
Reenth
Here we add "nth" at the end, and generate Ciphertext Based on the column order given by the keyword. The keyword indicates the order of the selected columns. The keyword is "Badboy ", because the minimum letter A is located in the second column, we first extract the 2nd columns in the plain text, that is, "eyde" is placed in the ciphertext, And the next minimum letter is B (and contains two B, first, select the top column), which is located in the first column. Therefore, extract "mirror R" and put it into the ciphertext. Then, the column contains 4th columns, 3rd columns, 5th columns, and 6th columns. That is, the extracted column order is, and 6, so the ciphertext is "eydemo-rthanmektetoeeoth ":
Eyde
Region R
Than
Mekt
Etde
Eoth
The keyword and ciphertext are given in sequence, and the plaintext must be output. In addition, the keywords and ciphertext are composed of uppercase letters. The keywords cannot exceed 10 characters, and the ciphertext cannot exceed 100 characters.
[Simple Analysis]
A classic simple question. After reading the question, I already know that this question must be AC. What else can I explain? The idea is simple. One character array stores ciphertext, And the other character array stores plaintext, but logically we regard them as "two-dimensional arrays ", for example, the plain text above is plaintext [4] [6], and the ciphertext is regarded as line [6] [4];
Assume char s [m] [N];
Then s [I] [J] is converted into a one-dimensional array, that is, s [I * n + J];
During decryption, each row of the ciphertext is processed in sequence, and the index "Index" of the smallest and highest letter in the keyword is selected from the keyword ", then copy the ciphertext [current row] To the plaintext "Index" column. To process repeated letters, we also need to introduce a flag array to mark whether the letter has been selected.
AllCodeAs follows:
Code_1799_crypto_columns
/* Zoj 1799-Crypto columns decryption */
# Include<Stdio. h>
# Include<String. H>
# Include<Stdlib. h>
/* Keyboard ing, no more than 10 characters, all uppercase letters! */
Char Keys [ 11 ];
Char Flag [ 11 ]; /* Indicates whether the column has been processed */
Char Line [ 101 ]; /* Ciphertext */
Char Plaintext [ 101 ]; /* Plaintext */
/* Decrypt ciphertext */
Void Decrypt ()
{
Int I, index, column, Count, keyslength, linelength;
Char C;
/* Clear flag */
Memset (flag, 0 , Sizeof (FLAG ));
/* Number of characters to be processed each time */
Linelength = Strlen (line ); /* Total number of ciphertext characters */
Keyslength = Strlen (KEYS ); /* Number of keys, that is, the number of Columns */
Count = Linelength / Keyslength;
Plaintext [linelength] = 0 ; /* Null-terminated */
/* Find the smallest letter */
For (Column = 0 ; Column < Keyslength; column ++ )
{
C = ' Z ' + 1 ;
For (I = 0 ; I < Keyslength; I ++ )
If (Flag [I] = 0 && Keys [I] < C)
C = Keys [I], Index = I;
/* Set the marker that the column has been processed */
Flag [Index] = 1 ;
/* Process the index column! */
/* Copy this line of ciphertext to the index column of plaintext */
For (I = 0 ; I < Count; I ++ )
{
/* Index column in plaintext */
Plaintext [keyslength * I + Index] = Line [count * Column + I];
}
}
}
Int Main ()
{
While ( 1 )
{
Gets (KEYS );
If (Strcmp (keys, " Theend " ) = 0 ) Break ;
Gets (line );
Decrypt ();
Printf ( " % S \ n " , Plaintext );
}
}
PS: This solution also ranks second in terms of memory usage of kb.