#K59432. Message Encoder and Decoder
Message Encoder and Decoder
Message Encoder and Decoder
You are given a message consisting of uppercase English letters (A-Z) and spaces. Your task is to encode or decode this message using a specific algorithm.
Encoding Process
For each character in the original message at 0-based index \(i\):
- If the character is an uppercase letter, its 0-based alphabetical index is computed as \(\text{pos} = \text{ord}(\text{char}) - \text{ord}('A')\) (i.e., A = 0, B = 1, ..., Z = 25).
- If the character is a space, it is assigned the index \(26\).
- Then, add \(i+1\) to \(\text{pos}\) and take the result modulo 27: \(\text{new_pos} = (\text{pos} + (i+1)) \bmod 27\).
- Map \(\text{new_pos}\) back to a character: indices 0 to 25 correspond to A-Z and index 26 corresponds to a space.
Decoding Process
The decoding process is the reverse operation. For each character in the encoded message at 0-based index \(i\):
- Convert the character to its corresponding index (A-Z: 0-25, space: 26).
- Subtract \(i+1\) from this index and then take modulo 27: \(\text{orig_pos} = (\text{pos} - (i+1)) \bmod 27\).
- Map \(\text{orig_pos}\) back to a character (0-25: A-Z, 26: space).
The input begins with a single letter indicating the mode: E for encoding and D for decoding, followed by the message in the next line. Your program must read from stdin
and output the resulting message to stdout
.
inputFormat
The first line of input contains a single character that is either E
or D
:
- E: Encode the given message.
- D: Decode the given message.
The second line contains the message. The message consists only of uppercase English letters and spaces. The length of the message can be zero or more.
outputFormat
Output a single line containing the resulting message after performing the encode or decode process.
## sampleE
HELLO WORLD
HFNOSXZISUI
</p>