結果

問題 No.1367 文字列門松
ユーザー RheoTommyRheoTommy
提出日時 2021-01-29 21:26:49
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 4,086 bytes
コンパイル時間 1,425 ms
コンパイル使用メモリ 156,836 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 14:41:04
合計ジャッジ時間 2,488 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_macros)]
#![allow(dead_code)]
#![allow(unused_imports)]

// # ファイル構成
// - use宣言
// - libモジュール
// - main関数
// - basicモジュール
//
// 常に使うテンプレートライブラリはbasicモジュール内にあります。
// 問題に応じて使うライブラリlibモジュール内にコピペしています。
// ライブラリのコードはこちら → https://github.com/RheoTommy/at_coder
// Twitterはこちら → https://twitter.com/RheoTommy

use std::collections::*;
use std::io::{stdout, BufWriter, Write};

use crate::basic::*;
use crate::lib::*;

pub mod lib {
    pub fn lcs<T: Eq + Clone>(s: &[T], t: &[T]) -> (usize, Vec<T>) {
        let mut dp = vec![vec![0; t.len() + 2]; s.len() + 2];
        for i in 0..=s.len() {
            for j in 0..=t.len() {
                if i < s.len() && j < t.len() && s[i] == t[j] {
                    dp[i + 1][j + 1] = (dp[i + 1][j + 1]).max(dp[i][j] + 1);
                }
                dp[i + 1][j + 1] = (dp[i + 1][j + 1]).max(dp[i][j]);
                dp[i + 1][j] = (dp[i + 1][j]).max(dp[i][j]);
                dp[i][j + 1] = (dp[i][j + 1]).max(dp[i][j]);
            }
        }
        let mut res = Vec::with_capacity(dp[s.len()][t.len()]);
        let mut i = s.len();
        let mut j = t.len();
        while dp[i][j] != 0 {
            if i > 0 && dp[i - 1][j] == dp[i][j] {
                i -= 1;
                continue;
            } else if j > 0 && dp[i][j - 1] == dp[i][j] {
                j -= 1;
                continue;
            } else {
                res.push(s[i - 1].clone());
                i -= 1;
                j -= 1;
            }
        }
        res.reverse();
        (dp[s.len()][t.len()], res)
    }
}

fn main() {
    let out = stdout();
    let mut writer = BufWriter::new(out.lock());
    let mut sc = Scanner::new();
    let s = sc.next_chars();
    let t = "kadomatsu".chars().collect::<Vec<_>>();
    let (l, _) = lcs(&s, &t);
    let ans = if l == s.len() { "Yes" } else { "No" };
    writeln!(writer, "{}", ans).unwrap();
}

pub mod basic {
    pub const U_INF: u128 = 1 << 60;
    pub const I_INF: i128 = 1 << 60;

    pub struct Scanner {
        buf: std::collections::VecDeque<String>,
        reader: std::io::BufReader<std::io::Stdin>,
    }

    impl Scanner {
        pub fn new() -> Self {
            Self {
                buf: std::collections::VecDeque::new(),
                reader: std::io::BufReader::new(std::io::stdin()),
            }
        }

        fn scan_line(&mut self) {
            use std::io::BufRead;
            let mut flag = 0;
            while self.buf.is_empty() {
                let mut s = String::new();
                self.reader.read_line(&mut s).unwrap();
                let mut iter = s.split_whitespace().peekable();
                if iter.peek().is_none() {
                    if flag >= 5 {
                        panic!("There is no input!");
                    }
                    flag += 1;
                    continue;
                }

                for si in iter {
                    self.buf.push_back(si.to_string());
                }
            }
        }

        pub fn next<T: std::str::FromStr>(&mut self) -> T {
            self.scan_line();
            self.buf
                .pop_front()
                .unwrap()
                .parse()
                .unwrap_or_else(|_| panic!("Couldn't parse!"))
        }

        pub fn next_usize(&mut self) -> usize {
            self.next()
        }

        pub fn next_int(&mut self) -> i128 {
            self.next()
        }

        pub fn next_uint(&mut self) -> u128 {
            self.next()
        }

        pub fn next_chars(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }

        pub fn next_string(&mut self) -> String {
            self.next()
        }

        pub fn next_char(&mut self) -> char {
            self.next()
        }

        pub fn next_float(&mut self) -> f64 {
            self.next()
        }
    }
}
0