CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:ABC-poisto
Sender:Hennkka
Submission time:2020-10-18 11:53:36 +0300
Language:Rust
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED42
#2ACCEPTED58
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2ACCEPTED0.01 s2details

Code

use std::io::BufRead;
use std::mem::swap;

fn solve_abc(a: usize, b: usize, c: usize) -> usize {
    let mut a = a;
    let mut b = b;
    let mut c = c;

    if a < b {
        swap(&mut a, &mut b);
    };
    if b < c {
        swap(&mut b, &mut c);
    };
    if a < b {
        swap(&mut a, &mut b);
    };

    if b == 0 {
        0
    } else {
        2 + solve_abc(a - 1, b - 1, c)
    }
}

fn solve(x: &str) -> usize {
    let a = x.chars().filter(|c| *c == 'A').count();
    let b = x.chars().filter(|c| *c == 'B').count();
    let c = x.chars().filter(|c| *c == 'C').count();
    solve_abc(a, b, c)
}

fn main() {
    let stdin = std::io::stdin();
    let stdin = stdin.lock();
    let mut lines = stdin.lines();

    let t: usize = lines.next().unwrap().unwrap().parse().unwrap();
    for _ in 0..t {
        let x = lines.next().unwrap().unwrap();
        println!("{}", solve(&x));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sample() {
        assert_eq!(solve("AB"), 2);
        assert_eq!(solve("CABAC"), 4);
        assert_eq!(solve("AAAA"), 0);
        assert_eq!(solve("AABBCC"), 6);
    }
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
100
CABC
BABCCBCA
CBBCBBAC
ACAA
...

correct output
4
8
8
2
2
...

user output
4
8
8
2
2
...
Truncated

Test 2

Group: 2

Verdict: ACCEPTED

input
100
CCAAACBCBBCCACBBBCCACCCBABBCAB...

correct output
48
4
4
96
70
...

user output
48
4
4
96
70
...
Truncated