114. Viginere Cipher

Codeforces
IDCF10269114
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
So I think the title explains it. Encrypt a given phrase using the vigenere cipher with a key that will be provided. The vigenere cipher is actually quite easy to use, just look at the image below. The left side is where the plain text characters go and the top is where the key letters go. The first letter of the plain text goes with the first letter of the key and all you do is draw a line across for the plain letter and a line down for the key letter. Where the two lines intersect is the new encrypted letter. Then you pair the second letter of the plain text with the second letter of the key and so on. Once you have reach the last letter of the key, you just go to the start of the key again. Also don't encrypt spaces, so just skip over them. The first line will be the plain text and the second line will be the key. Print the newly encrypted text. ## Input The first line will be the plain text and the second line will be the key. ## Output Print the newly encrypted text. [samples]
**Definitions** Let $ P = p_1 p_2 \dots p_m $ be the plaintext string over the alphabet $ \Sigma = \{A, B, \dots, Z\} $, possibly containing spaces. Let $ K = k_1 k_2 \dots k_\ell $ be the key string over $ \Sigma $, with no spaces. Let $ \text{idx}(c) = c - A $ for $ c \in \Sigma $, mapping letters to $ \{0, 1, \dots, 25\} $. **Constraints** 1. Spaces in $ P $ are preserved and not encrypted. 2. Only alphabetic characters (A–Z) in $ P $ and $ K $ are processed; assume uppercase. 3. The key is repeated cyclically: for position $ i $ in $ P $, use key index $ j = ((i' - 1) \mod \ell) + 1 $, where $ i' $ is the count of non-space characters up to position $ i $. **Objective** For each character $ p_i \in P $: - If $ p_i $ is a space, output a space. - Else, compute the encrypted character: $$ e_i = \left( \text{idx}(p_i) + \text{idx}(k_{j}) \right) \mod 26 $$ where $ j = \left( \left( \sum_{r=1}^{i-1} \mathbf{1}_{p_r \neq \text{space}} \right) \mod \ell \right) + 1 $, then output $ \text{char}(e_i + A) $. Output the resulting encrypted string.
API Response (JSON)
{
  "problem": {
    "name": "114. Viginere Cipher",
    "description": {
      "content": "So I think the title explains it. Encrypt a given phrase using the vigenere cipher with a key that will be provided. The vigenere cipher is actually quite easy to use, just look at the image below. Th",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10269114"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "So I think the title explains it. Encrypt a given phrase using the vigenere cipher with a key that will be provided. The vigenere cipher is actually quite easy to use, just look at the image below. Th...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ P = p_1 p_2 \\dots p_m $ be the plaintext string over the alphabet $ \\Sigma = \\{A, B, \\dots, Z\\} $, possibly containing spaces.  \nLet $ K = k_1 k_2 \\dots k_\\ell $ be the key str...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments