//#[derive_readable] #[derive(Debug, Clone)] struct Problem { ss: Vec, } impl Problem { fn read() -> Problem { input! { ss: Chars, } Problem { ss } } fn solve(&self) -> Answer { let ss = &self.ss; let mut stack = Vec::new(); for &s in ss { //dbg!(stack.iter().collect::()); if s == '>' { // stack の末尾が <=...= になっているか調べる let start_opt: Option = { if stack.is_empty() { None } else { let mut current = stack.len() - 1; // = が出てくるまでデクリメント while stack[current] == '=' && current > 0 { current -= 1; } if current == 0 { None } else if current == stack.len() - 1 { // = がない None } else if stack[current] == '<' { Some(current) } else { None } } }; if let Some(start) = start_opt { // stack[start..] を消す for _ in 0..(stack.len() - start) { //dbg!("pop"); stack.pop(); } } else { stack.push(s); } } else { stack.push(s); } } let ans = stack.len() as i64; Answer { ans } } #[allow(dead_code)] fn solve_naive(&self) -> Answer { todo!(); // let ans = 0; // Answer { ans } } } #[derive(Clone, Debug, PartialEq, Eq)] struct Answer { ans: i64, } impl Answer { fn print(&self) { println!("{}", self.ans); } } fn main() { Problem::read().solve().print(); } #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; #[allow(unused_imports)] use rand::{rngs::SmallRng, seq::SliceRandom, *}; #[test] fn test_problem() { assert_eq!(1 + 1, 2); } #[allow(dead_code)] #[derive(Debug)] struct WrongTestCase { problem: Problem, main_ans: Answer, naive_ans: Answer, } #[allow(dead_code)] fn check(p: &Problem) -> Option { let main_ans = p.solve(); let naive_ans = p.solve_naive(); if main_ans != naive_ans { Some(WrongTestCase { problem: p.clone(), main_ans, naive_ans, }) } else { None } } #[allow(dead_code)] fn make_random_problem(rng: &mut SmallRng) -> Problem { todo!() // let n = rng.gen_range(1..=10); // let p = Problem { _a: n }; // println!("{:?}", &p); // p } #[allow(unreachable_code)] #[test] fn test_with_naive() { let num_tests = 0; let max_wrong_case = 10; // この件数間違いが見つかったら打ち切り let mut rng = SmallRng::seed_from_u64(42); // let mut rng = SmallRng::from_entropy(); let mut wrong_cases: Vec = vec![]; for _ in 0..num_tests { let p = make_random_problem(&mut rng); let result = check(&p); if let Some(wrong_test_case) = result { wrong_cases.push(wrong_test_case); } if wrong_cases.len() >= max_wrong_case { break; } } if !wrong_cases.is_empty() { for t in &wrong_cases { println!("{:?}", t.problem); println!("main ans : {:?}", t.main_ans); println!("naive ans: {:?}", t.naive_ans); println!(); } println!("{} cases are wrong.", wrong_cases.len()); panic!(); } } } // ====== import ====== #[allow(unused_imports)] use proconio::{ derive_readable, fastout, input, marker::{Bytes, Chars, Usize1}, }; #[allow(unused_imports)] use std::cmp::Reverse; #[allow(unused_imports)] use std::collections::{BinaryHeap, HashMap, HashSet}; // ====== output func ====== #[allow(unused_imports)] use print_vec::*; pub mod print_vec { use proconio::fastout; #[fastout] pub fn print_vec(arr: &[T]) { for a in arr { println!("{:?}", a); } } #[fastout] pub fn print_vec_1line(arr: &[T]) { let msg = arr .iter() .map(|x| format!("{:?}", x)) .collect::>() .join(" "); println!("{}", msg); } #[fastout] pub fn print_vec2(arr: &Vec>) { for row in arr { let msg = row .iter() .map(|x| format!("{:?}", x)) .collect::>() .join(" "); println!("{}", msg); } } pub fn print_bytes(bytes: &[u8]) { let msg = String::from_utf8(bytes.to_vec()).unwrap(); println!("{}", msg); } pub fn print_chars(chars: &[char]) { let msg = chars.iter().collect::(); println!("{}", msg); } #[fastout] pub fn print_vec_bytes(vec_bytes: &[Vec]) { for row in vec_bytes { let msg = String::from_utf8(row.to_vec()).unwrap(); println!("{}", msg); } } } #[allow(unused)] fn print_yesno(ans: bool) { let msg = if ans { "Yes" } else { "No" }; println!("{}", msg); } // ====== snippet ====== use mod_stack::*; pub mod mod_stack { #[derive(Clone, Debug, PartialEq, Eq)] pub struct Stack { raw: Vec, } impl Stack { pub fn new() -> Self { Stack { raw: Vec::new() } } pub fn push(&mut self, value: T) { self.raw.push(value) } pub fn pop(&mut self) -> Option { self.raw.pop() } pub fn peek(&self) -> Option<&T> { self.raw.last() } pub fn is_empty(&self) -> bool { self.raw.is_empty() } pub fn len(&self) -> usize { self.raw.len() } } impl Default for Stack { fn default() -> Self { Self::new() } } }