Submission #2522756


Source Code Expand

use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

struct Solver {
    s: Vec<char>,
    skip: Vec<Vec<usize>>,
    memo: Vec<usize>,
}

impl Solver {
    fn new() -> Solver {
        let s = read::<String>().chars().collect::<Vec<char>>();
        let n = s.len();
        let mut skip = vec![vec![n + 10; 26]; n + 1];
        for i in (0..n).rev() {
            let c = (s[i] as u8 - 'a' as u8) as usize;
            skip[i] = skip[i + 1].clone();
            skip[i][c] = i;
        }
        Solver {
            s: s,
            skip: skip,
            memo: vec![n + 10; n + 10],
        }
    }
    fn calc(&mut self, pos: usize) -> usize {
        let n = self.s.len();
        if pos >= n + 10 {
            return 0;
        }
        if self.memo[pos] < n + 10 {
            return self.memo[pos];
        }
        let mut ret = n;
        for c in 0..26 {
            let npos = 1 + self.skip[pos][c];
            let nret = 1 + self.calc(npos);
            // println!("{} {} {}", c, npos, nret);
            ret = std::cmp::min(ret, nret);
        }
        self.memo[pos] = ret;
        ret
    }
    fn solve(&mut self) {
        let n = self.s.len();
        let len = self.calc(0);
        let mut ans = vec![];
        let mut pos = 0;
        while pos < n {
            for c in 0..26 {
                let npos = 1 + self.skip[pos][c];
                let nlen = self.calc(npos);
                if 1 + nlen + ans.len() == len {
                    ans.push(c);
                    pos = npos;
                    break;
                }
            }
        }
        for &c in ans.iter() {
            let v = (c as u8 + 'a' as u8) as char;
            print!("{}", v);
        }
        println!("");
    }
}

fn main() {
    let mut solver = Solver::new();
    solver.solve();
}

Submission Info

Submission Time
Task E - Don't Be a Subsequence
User cos
Language Rust (1.15.1)
Score 600
Code Size 2256 Byte
Status AC
Exec Time 80 ms
Memory 57980 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 36
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 3.txt, 30.txt, 4.txt, 5.txt, 6.txt, 7.txt, 8.txt, 9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
1.txt AC 2 ms 4352 KB
10.txt AC 79 ms 57852 KB
11.txt AC 79 ms 57980 KB
12.txt AC 78 ms 57852 KB
13.txt AC 79 ms 57852 KB
14.txt AC 79 ms 57980 KB
15.txt AC 79 ms 57852 KB
16.txt AC 79 ms 57852 KB
17.txt AC 79 ms 57852 KB
18.txt AC 80 ms 57852 KB
19.txt AC 80 ms 57852 KB
2.txt AC 2 ms 4352 KB
20.txt AC 80 ms 57852 KB
21.txt AC 80 ms 57980 KB
22.txt AC 80 ms 57980 KB
23.txt AC 80 ms 57852 KB
24.txt AC 78 ms 57980 KB
25.txt AC 78 ms 57852 KB
26.txt AC 78 ms 57980 KB
27.txt AC 78 ms 57852 KB
28.txt AC 79 ms 57980 KB
29.txt AC 79 ms 57980 KB
3.txt AC 5 ms 6396 KB
30.txt AC 78 ms 57980 KB
4.txt AC 5 ms 6396 KB
5.txt AC 38 ms 31100 KB
6.txt AC 39 ms 31100 KB
7.txt AC 79 ms 57852 KB
8.txt AC 79 ms 57852 KB
9.txt AC 79 ms 57852 KB
sample1.txt AC 2 ms 4352 KB
sample2.txt AC 2 ms 4352 KB
sample3.txt AC 2 ms 4352 KB