Problem G
Love Letter
Languages
en
sv
Mio and Sara come from two very different families. In Mio’s family, everyone wears hats shaped like rectangles, while in Sara’s family, everyone wears triangular-shaped hats. This difference in taste has led to feuds that have lasted for generations. Despite this, Mio and Sara happened to meet one day and fell in love at first sight.
Since it is difficult for them to meet, they instead send messages to each other to communicate. Because they are very secretive, we do not know exactly how the devices they use to communicate work. However, we do know a few things: when Sara wants to send a message, she types it in as a sequence of ones and zeros into her device, and then it sends the message. Unfortunately, bad weather can cause problems. When the machine sends ones and zeros during bad weather, it sends the exact opposite. For example, if the weather is bad and the machine tries to send a 0, it will send a 1 instead.
Sara has observed that every time she sends a message, there is bad weather exactly once during a contiguous period of time. This means that if she tries to send a particular message, the message that the recipient receives will be incorrect in such a way that, in a specific interval (which can be of size 1 or more, but not zero), all zeros become ones and all ones become zeros. We call this the interval being corrupted.
For example: if Sara sends 11101001, it is possible that the weather
causes the following corruption: 11101001
Let the message Sara wants to send be denoted by
Your task is to develop the system for Sara and Mio so that
they can communicate flawlessly. You do this by implementing
two functions, encode and decode. The function encode will be called with
Afterward, Mio receives the string
To score points on this task, the system must work.
Therefore, decode must always be able
to find
Implementation
Your solution to the problem must be written in one of the following languages: C++, Java, Python, or Rust. Below is a description of what the implementation looks like in C++. In most other languages, it works similarly. You can download a reference implementation for your language in the attachments at the bottom of the page.
You must not create a main function.
Instead, you should implement the following functions:
-
string encode(string S)
-
The parameter
is the message Sara wants to send to Mio. consists only of the characters 0 and 1. In all test cases, consists of exactly 100 characters. -
The return value should be
, the message Sara sends through the machine. must contain at least one characters and may only consist of the characters 0 and 1.
-
-
After sending
, an interval will be corrupted during transmission. -
string decode(string M)
-
The parameter
is the message Mio receives. This will be after an interval has been corrupted. Therefore, consists only of the characters 0 and 1. -
The return value should be
, the message that encode was originally called with. If the return value is the same as , you get the correct answer for the test case.
-
encode and decode can only
communicate through the parameters they are called with,
Reference Implementations
All reference implementations in the attachments implement
the following protocol: encode sends
each letter in
It is important that your solution does not read or write from standard input/output. If your solution does this, you will likely get a strange error message. For some languages, the filename is important, so do not change it. Also, do not change the names of functions and classes if they exist. Rust requires extra steps to work, which you can read about in the file rust/readme.txt in karleksbrev.zip.
Scoring
Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.
Let
It is also given that
Group |
Points |
Constraints |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Input Format for the Example Judge
The example judge reads input in the following format:
-
A line with the integers
and . -
A line with the bit string
. -
A line with the integers
and . This is the interval in that the judge will corrupt.
Example Interaction
Consider the following input to the judge:
5 25 01011 1 3
When the example program is run, the following happens. Note that the example program does not solve the problem in this case.
Event |
Result |
encode(01011) is called |
encode returns 0000011111000001111111111 |
The judge corrupts the return value of encode(01011) |
The corrupted message becomes 0111011111000001111111111 |
decode(0111011111000001111111111) is called |
decode returns 11011 |
The judge compares 01011 and the return value of decode |
Since they differ, the solution gets “Wrong Answer” |