結果

問題 No.1542 ぽんぽんぽん ぽんぽんぽんぽん ぽんぽんぽん
ユーザー 57tggx57tggx
提出日時 2021-05-19 10:20:10
言語 Rust
(1.77.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 3,757 bytes
コンパイル時間 1,544 ms
コンパイル使用メモリ 162,012 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-17 20:47:20
合計ジャッジ時間 3,990 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 5 ms
6,940 KB
testcase_06 AC 47 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 108 ms
6,940 KB
testcase_09 AC 24 ms
6,940 KB
testcase_10 AC 117 ms
6,940 KB
testcase_11 AC 114 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 11 ms
6,944 KB
testcase_14 AC 64 ms
6,944 KB
testcase_15 AC 4 ms
6,944 KB
testcase_16 AC 37 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 4 ms
6,944 KB
testcase_20 AC 112 ms
6,944 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 44 ms
6,944 KB
testcase_23 AC 130 ms
6,944 KB
testcase_24 AC 128 ms
6,940 KB
testcase_25 AC 127 ms
6,944 KB
testcase_26 AC 127 ms
6,940 KB
testcase_27 AC 129 ms
6,940 KB
testcase_28 AC 130 ms
6,940 KB
testcase_29 AC 133 ms
6,944 KB
testcase_30 AC 131 ms
6,940 KB
testcase_31 AC 131 ms
6,944 KB
testcase_32 AC 132 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),* $(,)*) => {
        #[cfg(debug_assertions)]
        eprintln!(concat!($("| ", stringify!($a), "={:?} "),*, "|"), $(&$a),*);
    };
}

fn main() {
    let Input { n, s } = Input::read(std::io::stdin().lock());

    let mut dp: Vec<Vec<(i32, i32, i32)>> = vec![vec![(0, 0, 0); n + 1]; n + 1];
    // dp[i][j].0: s[i..j] の末尾に on を付け足したときの pon 数
    // dp[i][j].1: s[i..j] の末尾に n を付け足したときの pon 数
    // dp[i][j].2: s[i..j] の pon 数

    // 0 <= i <= j <= n を満たす (i, j) を走査
    for j in 0..=n {
        for i in (0..=j).rev() {
            // s[i..j] を見るとき,
            // 0 <= k <= l < j を満たす s[k..l] と
            // i < k <= l <= j を満たす s[k..l] は
            // 全て既に見ている

            if i < j {
                match s[j - 1] {
                    'p' => chmax(1, &mut dp[i][j].0),
                    'o' => chmax(dp[i][j - 1].0, &mut dp[i][j].1),
                    'n' => chmax(dp[i][j - 1].1, &mut dp[i][j].2),
                    _ => unreachable!(),
                }
            }
            for k in 0..i {
                chmax(dp[k][i].0 + dp[i][j].2, &mut dp[k][j].0);
                chmax(dp[k][i].1 + dp[i][j].2, &mut dp[k][j].1);
                chmax(dp[k][i].2 + dp[i][j].2, &mut dp[k][j].2);
            }
        }
    }
    /*
    for row in &dp {
        eprintln!("{}", row.iter().map(|x| format!("{:?}", x)).collect::<Vec<_>>().join(" "));
    }
    */
    let mut ans = None;
    for i in 0..n {
        let former = dp[0][i].2;
        let latter = dp[i][n].2;
        if former > 0 && latter > 0 {
            ans = ans.max(Some(former + latter - 2));
        }
    }
    println!("{}", ans.unwrap_or(-1));
}

fn chmax<T: PartialOrd>(x: T, y: &mut T) {
    if &x > y {
        *y = x;
    }
}

struct Input {
    n: usize,
    s: Vec<char>,
}

impl Input {
    fn read<T: std::io::BufRead>(mut input: T) -> Input {
        let n = {
            let mut buffer = String::new();
            input.read_line(&mut buffer).unwrap();
            match &split(&buffer).unwrap()[..] {
                [n] => n.parse().unwrap(),
                _ => panic!("input format error: N"),
            }
        };
        let s = {
            let mut buffer = String::new();
            input.read_line(&mut buffer).unwrap();
            let split = split(&buffer).unwrap();
            assert_eq!(split.len(), 1, "input format error: S");
            split[0].chars().collect::<Vec<_>>()
        };
        assert!(matches!(n, 1..=500));
        assert_eq!(n, s.len());
        Input { n: n, s: s }
    }
}

fn split(s: &str) -> Option<Vec<&str>> {
    enum State {
        Word(usize),
        Space,
        End,
    }
    let mut state = State::Word(0);
    let mut ret = Vec::new();
    for (i, c) in s.char_indices() {
        let prev = match state {
            State::End => return None,
            State::Word(i) => i,
            State::Space => {
                state = State::Word(i);
                i
            }
        };
        if c == ' ' || c == '\n' {
            ret.push(&s[prev..i]);
            state = if c == ' ' { State::Space } else { State::End };
        }
    }
    matches!(state, State::End).then(|| ret)
}

#[test]
fn test_split() {
    assert_eq!(split("word\n"), Some(vec!["word"]));
    assert_eq!(
        split("many words separated\n"),
        Some(vec!["many", "words", "separated"])
    );
    assert_eq!(
        split(" extra  spaces \n"),
        Some(vec!["", "extra", "", "spaces", ""])
    );
    assert_eq!(split("no line feed"), None);
    assert_eq!(split("extra characters\nafter line feed"), None);
}
0