#T783. 密码破译

密码破译

Description

Julius Caesar lived in an era fraught with danger and intrigue, where survival was the greatest challenge. To secure military communications, he devised one of the earliest cryptographic systems.

Assume you are an officer in Caesar's legion tasked with decrypting his messages and relaying them to your general. The encryption method is as follows: each letter in the original message is replaced by the letter five positions ahead of it in the alphabet (e.g., every letter 'A' in the original message is replaced with 'F'). Your job is to reverse this process and recover the original message.

Encrypted letters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Original letters: V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Note: Only letters are substituted; all other non-alphabetic characters remain unchanged. Additionally, all letters in the original message are uppercase.

Input Format

The input consists of no more than 100 datasets. There are no blank lines between datasets. Each dataset comprises three parts:

  1. Start line: START
  2. Encrypted message: A single line containing 1 to 200 characters, representing an encrypted message from Caesar.
  3. End line: END

After the last dataset, there is an additional line: ENDOFINPUT

Output Format

For each dataset, output a single line containing the decrypted original message.

Example Input

START  
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX  
END  
START  
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ  
END  
START  
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ  
END  
ENDOFINPUT  

Example Output

IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES  
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME  
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE  

Note

The decryption involves shifting each letter five positions backward in the alphabet (e.g., 'F' becomes 'A', 'G' becomes 'B', etc.). Letters wrap around the alphabet, so 'A' maps to 'V', 'B' to 'W', and so on.

Solution Code

while True:  
    line = input().strip()  
    if line == "ENDOFINPUT":  
        break  
    if line == "START":  
        encrypted = input().strip()  
        end_line = input().strip()  
        decrypted = []  
        for c in encrypted:  
            if 'A' <= c <= 'Z':  
                original = chr((ord(c) - ord('A') - 5) % 26 + ord('A'))  
                decrypted.append(original)  
            else:  
                decrypted.append(c)  
        print(''.join(decrypted))  
START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

Hint

Note: The test data contains multiple messages, so pay attention to the input method.

The gets function has been completely removed in the latest versions because it cannot limit the input length, causing a large number of historical buffer overflow vulnerabilities. Please use the fgets function instead. Alternatively, you can use the following macro definition as a replacement:

#define gets(S) fgets(S,sizeof(S),stdin)

Source

CodesOnline