tinycodinglm / cp2 /cp-0024.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.88 kB
cp-practice-sim trace
System You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Reply with code only; no explanation.

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Reply with code only; no explanation.

User 2026-11-07T21:20:03.537Z

Drifting Caesar Cipher

Topic: String manipulation and parsing | Difficulty: easy

Problem

Encrypt a text as follows. Number the letters of the text (ignoring every other character) starting from 0. The letter with number i is shifted forward in the alphabet by (k + i) mod 26 positions, wrapping from z to a, preserving whether it is upper or lower case. All non-letter characters stay unchanged. Print the encrypted text.

Input

The first line contains the integer k. The second line contains the text.

Output

Print the encrypted text on one line.

Constraints

  • 0 <= k <= 10^9
  • 1 <= text length <= 2*10^5
  • the text contains printable ASCII characters

Examples

Example 1

Input:

markdown
1
abc xyz

Output:

markdown
bdf bdf

Note: Shifts are 1,2,3,4,5,6; the space is skipped when numbering letters.

Example 2

Input:

markdown
0
Hello, World!

Output:

markdown
Hfnos, Buytm!

Solve this in Rust, TypeScript, C++17, one complete program per language. Code only, no explanation.

Assistant 2026-11-07T21:20:05.511Z

Rust

rust
use std::io::*;
fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    let mut lines = s.split('\n');
    let k: u64 = lines.next().unwrap().trim().parse().unwrap();
    let text = lines.next().unwrap_or("").trim_end_matches('\r');
    let mut out = String::new();
    let mut i: u64 = 0;
    for ch in text.chars() {
        if ch.is_ascii_alphabetic() {
            let base = if ch.is_ascii_lowercase() { b'a' } else { b'A' };
            let sh = ((k + i) % 26) as u8;
            out.push(((ch as u8 - base + sh) % 26 + base) as char);
            i += 1;
        } else {
            out.push(ch);
        }
    }
    println!("{}", out);
}

TypeScript

typescript
declare const require: any;
const lines: string[] = require('fs').readFileSync(0, 'utf8').split('\n');
const k = parseInt(lines[0].trim());
const text = (lines[1] || '').replace(/\r$/, '');
let out = '';
let i = 0;
for (const ch of text) {
  const c = ch.charCodeAt(0);
  const lower = c >= 97 && c <= 122;
  const upper = c >= 65 && c <= 90;
  if (lower || upper) {
    const base = lower ? 97 : 65;
    out += String.fromCharCode(((c - base + ((k + i) % 26)) % 26) + base);
    i++;
  } else {
    out += ch;
  }
}
console.log(out);

C++17

cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    long long k;
    string text;
    cin >> k;
    getline(cin, text);
    getline(cin, text);
    if (!text.empty() && text.back() == '\r') text.pop_back();
    long long i = 0;
    for (char &ch : text) {
        bool lo = ch >= 'a' && ch <= 'z', up = ch >= 'A' && ch <= 'Z';
        if (lo || up) {
            char base = lo ? 'a' : 'A';
            ch = (char)((ch - base + (k + i) % 26) % 26 + base);
            i++;
        }
    }
    printf("%s\n", text.c_str());
}