結果

問題 No.1542 ぽんぽんぽん ぽんぽんぽんぽん ぽんぽんぽん
ユーザー 57tggx57tggx
提出日時 2021-05-21 01:08:43
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,484 bytes
コンパイル時間 4,673 ms
コンパイル使用メモリ 155,904 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 14:12:38
合計ジャッジ時間 1,633 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 0 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 1 ms
5,376 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1 ms
5,376 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `n`
  --> main.rs:18:17
   |
18 |     let Input { n, s } = Input::read(std::io::stdin().lock());
   |                 ^ help: try ignoring the field: `n: _`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted

ソースコード

diff #

// これは嘘解法
// WA にならなければいけない!

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

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

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

    let mut stack = Vec::new();
    let mut ans = None;

    let mut entire_count = 0;
    let mut entire_separate = 0;

    for c in &s {
        match c {
            'p' => stack.push((0, 0, 0)),
            'o' => {
                if let Some(&(0, _, _)) = stack.last() {
                    stack.last_mut().unwrap().0 = 1;
                }
            }
            'n' => {
                if let Some(&(1, count, separate)) = stack.last() {
                    // debug!(count, separate);
                    stack.pop();
                    if separate >= 2 {
                        ans = ans.max(Some(count));
                    }
                    if let Some(last) = stack.last_mut() {
                        last.1 += count + 1;
                        last.2 += 1;
                    } else {
                        entire_count += count + 1;
                        entire_separate += 1;
                    }
                }
            }
            _ => unreachable!(),
        }
        // debug!(stack);
    }
    // debug!(entire_count, entire_separate);
    if entire_separate >= 2 {
        ans = ans.max(Some(entire_count));
    }
    println!(
        "{}",
        match ans {
            Some(ans) => ans - 2,
            None => -1,
        }
    );
}

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