CSES - Datatähti 2024 loppu - Results
Submission details
Task:Palindromi
Sender:EmuBird
Submission time:2024-01-20 15:06:33 +0200
Language:Rust
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.00 sdetails

Code

use std::{io::stdin};

fn main() {
    let stdin = stdin();
    let (n, k): (usize, usize) = {
        let mut s = String::new();
        stdin.read_line(&mut s).unwrap();
        let mut l = s.trim_end().split_whitespace();
        (l.next().unwrap().parse().unwrap(), l.next().unwrap().parse().unwrap())
    };

    let letters: Vec<char> = "abcdefghijklmnopqrstuvwxyz".chars().collect();

    let mut palindromi = String::with_capacity(n);

    for i in 0..(k/2) {
        palindromi.insert(i, letters[i % letters.len()]);
        palindromi.insert(palindromi.len() - 1 - i, letters[i % letters.len()]);
    }
    if k % 2 != 0 {
        palindromi.insert(k / 2, letters[(k / 2 + 1) % letters.len()]);
    }
    
    let mut offset: usize = if palindromi.len() > 0 { alphabet_index(palindromi.chars().last().unwrap()) + 1 } else { 0 };
    if palindromi.len() > 0 && n > k {
        let first_palindromi_char = palindromi.chars().next().unwrap();
        let first_palindromi_char_index: usize = alphabet_index(first_palindromi_char);
        if (n - k - 1 + offset) % letters.len() == first_palindromi_char_index {
            offset = 1;
        }
    }

    let mut fill = String::with_capacity(n - k);
    for i in 0..(n - k) {
        fill.push(letters[(i + offset) % letters.len()]);
    }

    assert_ne!(fill.chars().last(), palindromi.chars().next());
    assert_eq!(fill.len() + palindromi.len(), n);

    println!("{}", fill + &*palindromi)
}


// Debugger crashes for some reason with letters.iter().position(|c| c == first_palindromi_char).unwrap();
fn alphabet_index(character: char) -> usize {
    let letters: Vec<char> = "abcdefghijklmnopqrstuvwxyz".chars().collect();

    let mut index = 0;
    for (i, c) in letters.iter().enumerate() {
        if *c == character {
            index = i;
            break;
        }
    }
    index
}

Test details

Test 1

Verdict: ACCEPTED

input
1 1

correct output
a

user output
b

Test 2

Verdict: ACCEPTED

input
2 1

correct output
ab

user output
cb

Test 3

Verdict: ACCEPTED

input
2 2

correct output
aa

user output
aa

Test 4

Verdict: ACCEPTED

input
3 1

correct output
abc

user output
cdb

Test 5

Verdict: ACCEPTED

input
3 2

correct output
aab

user output
baa

Test 6

Verdict: ACCEPTED

input
3 3

correct output
aaa

user output
aca

Test 7

Verdict: ACCEPTED

input
4 1

correct output
abca

user output
cdeb

Test 8

Verdict: ACCEPTED

input
4 2

correct output
aabc

user output
bcaa

Test 9

Verdict: ACCEPTED

input
4 3

correct output
aaab

user output
baca

Test 10

Verdict: ACCEPTED

input
4 4

correct output
aaaa

user output
abba

Test 11

Verdict: ACCEPTED

input
100 1

correct output
abcabcabcabcabcabcabcabcabcabc...

user output
cdefghijklmnopqrstuvwxyzabcdef...
Truncated

Test 12

Verdict: ACCEPTED

input
100 2

correct output
aabcabcabcabcabcabcabcabcabcab...

user output
bcdefghijklmnopqrstuvwxyzabcde...
Truncated

Test 13

Verdict: ACCEPTED

input
100 5

correct output
aaaaabcabcabcabcabcabcabcabcab...

user output
bcdefghijklmnopqrstuvwxyzabcde...
Truncated

Test 14

Verdict: ACCEPTED

input
100 10

correct output
aaaaaaaaaabcabcabcabcabcabcabc...

user output
bcdefghijklmnopqrstuvwxyzabcde...
Truncated

Test 15

Verdict: ACCEPTED

input
100 50

correct output
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

user output
bcdefghijklmnopqrstuvwxyzabcde...
Truncated

Test 16

Verdict: ACCEPTED

input
100 90

correct output
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

user output
bcdefghijkabcdefghijklmnopqrst...
Truncated

Test 17

Verdict: ACCEPTED

input
100 100

correct output
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

user output
abcdefghijklmnopqrstuvwxyzabcd...
Truncated