Submission #1390359


Source Code Expand

fn shortage(s: &String) -> (usize, usize) {
    let mut sum = 0;
    let mut ans = 0;
    for c in s.chars() {
        if c == '(' {
            sum += 1;
        } else {
            if sum == 0 {
                ans += 1;
            } else {
                sum -= 1;
            }
        }
    }
    (ans, sum)
}
fn main() {
    let n: i32 = readln();
    let s: String = readln();
    let (a, b) = shortage(&s);
    let ans = String::from_utf8(vec![b'('; a]).unwrap() + &s +
     &String::from_utf8(vec![b')'; b]).unwrap();
    println!("{}", ans);
}

// --- template ---
#[allow(unused_imports)]
use std::cmp::{max, min};
use std::collections::HashSet;

pub trait FromLn {
    fn fromln(s: &str) -> Self;
}
pub fn readln<T: FromLn>() -> T {
    let mut buf = String::new();
    let _ = ::std::io::stdin().read_line(&mut buf).unwrap();
    T::fromln(buf.trim())
}
pub fn readlns<T: FromLn>(n: usize) -> Vec<T> {
    let mut vs = vec![];
    for _ in 0..n { vs.push(readln()); }
    vs
}
macro_rules! fromln_primitives {
    ($($t:ty),*) => { $(
        impl FromLn for $t {
            fn fromln(s: &str) -> $t {
                s.parse().unwrap()
            }
        }
    )* }
}
fromln_primitives!(String, bool, f32, f64, isize, i8, i16, i32, i64, usize, u8, u16, u32, u64);
impl<T> FromLn for Vec<T> where T: FromLn {
    fn fromln(s: &str) -> Vec<T> {
        s.split_whitespace().map(T::fromln).collect()
    }
}
impl FromLn for Vec<char> {
    fn fromln(s: &str) -> Vec<char> {
        s.chars().collect()
    }
}
macro_rules! fromln_tuple {
    ($($t:ident),*) => {
        impl<$($t),*> FromLn for ($($t),*) where $($t: FromLn),* {
            fn fromln(s: &str) -> ($($t),*) {
                let mut it = s.split_whitespace();
                let t = ($($t::fromln(it.next().unwrap())),*);
                assert_eq!(it.next(), None);
                t
            }
        }
    }
}
fromln_tuple!(A, B);
fromln_tuple!(A, B, C);
fromln_tuple!(A, B, C, D);
fromln_tuple!(A, B, C, D, E);
fromln_tuple!(A, B, C, D, E, F);

Submission Info

Submission Time
Task D - Insertion
User ichyo
Language Rust (1.15.1)
Score 400
Code Size 2116 Byte
Status AC
Exec Time 2 ms
Memory 4352 KB

Compile Error

warning: unused variable: `n`, #[warn(unused_variables)] on by default
  --> ./Main.rs:18:9
   |
18 |     let n: i32 = readln();
   |         ^

warning: unused import: `std::collections::HashSet`, #[warn(unused_imports)] on by default
  --> ./Main.rs:29:5
   |
29 | use std::collections::HashSet;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 12
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All in1.txt, in2.txt, in3.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
in1.txt AC 2 ms 4352 KB
in2.txt AC 2 ms 4352 KB
in3.txt AC 2 ms 4352 KB
in4.txt AC 2 ms 4352 KB
in5.txt AC 2 ms 4352 KB
in6.txt AC 2 ms 4352 KB
in7.txt AC 2 ms 4352 KB
in8.txt AC 2 ms 4352 KB
in9.txt AC 2 ms 4352 KB
s1.txt AC 2 ms 4352 KB
s2.txt AC 2 ms 4352 KB
s3.txt AC 2 ms 4352 KB