// これは嘘解法 // WA にならなければいけない! #[allow(unused_macros)] macro_rules! debug { ($($a:expr),* $(,)*) => { #[cfg(debug_assertions)] eprintln!(concat!($("| ", stringify!($a), "={:?} "),*, "|"), $(&$a),*); }; } struct Input { n: usize, s: Vec, } 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(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::>() }; assert!(matches!(n, 1..=500)); assert_eq!(n, s.len()); Input { n: n, s: s } } } fn split(s: &str) -> Option> { 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); }