結果

問題 No.1542 ぽんぽんぽん ぽんぽんぽんぽん ぽんぽんぽん
ユーザー 57tggx57tggx
提出日時 2021-05-19 00:43:14
言語 Rust
(1.77.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 4,242 bytes
コンパイル時間 6,199 ms
コンパイル使用メモリ 146,560 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 20:47:52
合計ジャッジ時間 3,081 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 0 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 34 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 74 ms
5,376 KB
testcase_09 AC 16 ms
5,376 KB
testcase_10 AC 79 ms
5,376 KB
testcase_11 AC 80 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 8 ms
5,376 KB
testcase_14 AC 43 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 25 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 79 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 29 ms
5,376 KB
testcase_23 AC 86 ms
5,376 KB
testcase_24 AC 90 ms
5,376 KB
testcase_25 AC 93 ms
5,376 KB
testcase_26 AC 85 ms
5,376 KB
testcase_27 AC 85 ms
5,376 KB
testcase_28 AC 88 ms
5,376 KB
testcase_29 AC 92 ms
5,376 KB
testcase_30 AC 87 ms
5,376 KB
testcase_31 AC 89 ms
5,376 KB
testcase_32 AC 85 ms
5,376 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 last_p = {
        // last_p[i]: 0 <= k < i かつ s[k] = 'p' を満たす最大の k
        let mut v = vec![None; n + 1];
        let mut prev = None;
        for i in 0..n {
            v[i] = prev;
            if s[i] == 'p' {
                prev = Some(i);
            }
        }
        v[n] = prev;
        v
    };
    // debug!(last_p);
    let first_o = {
        // first_o[i]: i <= k < n かつ s[k] = 'o' を満たす最小の k
        let mut v = vec![None; n + 1];
        let mut prev = None;
        for i in (0..n).rev() {
            if s[i] == 'o' {
                prev = Some(i);
            }
            v[i] = prev;
        }
        v
    };
    // debug!(first_o);

    let mut dp: Vec<Vec<(i32, i32)>> = vec![vec![(0, 0); n + 1]; n + 1];
    // dp[i][j].0: s[i..j] の pon 数
    // dp[i][j].1: s[i..j] の末尾に n を付け足したときの pon 数
    for j in 0..=n {
        for i in (0..=j).rev() {
            if i < j {
                // dp[i][j - 1] から得られる dp[i][j]
                chmax(dp[i][j - 1].0, &mut dp[i][j].0);
                chmax(dp[i][j - 1].1, &mut dp[i][j].1);
                if s[j - 1] == 'n' {
                    chmax(dp[i][j - 1].1, &mut dp[i][j].0);
                }
            }
            // 配る
            if let (Some(lp), Some(fo)) = (last_p[i], first_o[j]) {
                chmax(dp[i][j].0 + 1, &mut dp[lp][fo + 1].1);
            }
            for k in 0..i {
                chmax(dp[k][i].0 + dp[i][j].1, &mut dp[k][j].1);
                chmax(dp[k][i].1 + dp[i][j].0, &mut dp[k][j].1);
            }
        }
    }
    /*
    for row in &dp {
        println!("{}", row.iter().map(|(x, y)| format!("({},{})", x, y)).collect::<Vec<_>>().join(" "));
    }
    */
    let mut ans = None;
    for i in 0..n {
        let former = dp[0][i].0;
        let latter = dp[i][n].0;
        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