結果

問題 No.3143 Colorless Green Parentheses Sleep Furiously
ユーザー Kana Nagata
提出日時 2025-05-17 01:40:25
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,260 bytes
コンパイル時間 12,239 ms
コンパイル使用メモリ 397,904 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-17 01:40:42
合計ジャッジ時間 15,171 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::input;

fn main() {
    input! {
        _n: usize,
        k: usize,
        s: String,
    }
    let ans = solve(k, s);
    if let Some(ans) = ans {
        println!("Yes");
        println!("{}", ans);
    } else {
        println!("No");
    }
}
fn solve(k: usize, s: String) -> Option<String> {
    let mut ans = String::new();
    let mut stack = vec![0];
    for c in s.chars() {
        match c {
            '(' => {
                if stack.last().map_or(false, |&i| i > 0) {
                    ans.push('+');
                }
                *stack.last_mut().unwrap() += 1;
                stack.push(0);
            }
            ')' => {
                let i = stack.pop().unwrap();
                match i {
                    0 => ans.push_str("1+1"),
                    1 => ans.push_str("+1"),
                    _ => (),
                }
            }
            _ => unreachable!(),
        }
        (stack.len() >= 1).then_some(())?;
        ans.push(c);
    }
    if stack.pop().unwrap() == 1 {
        ans.push_str("+1");
    }
    (stack.is_empty()).then_some(())?;
    let rest = k.checked_sub(ans.chars().filter(|&c| c == '1').count())?;
    for _ in 0..rest {
        ans.push_str("+1");
    }
    Some(ans)
}
0