結果
| 問題 |
No.1542 ぽんぽんぽん ぽんぽんぽんぽん ぽんぽんぽん
|
| コンテスト | |
| ユーザー |
57tggx
|
| 提出日時 | 2021-05-21 01:08:43 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,484 bytes |
| コンパイル時間 | 11,998 ms |
| コンパイル使用メモリ | 402,568 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-10-10 07:25:28 |
| 合計ジャッジ時間 | 13,374 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 2 WA * 28 |
コンパイルメッセージ
warning: unused variable: `n`
--> src/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
ソースコード
// これは嘘解法
// 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);
}
57tggx