結果
問題 |
No.3143 Colorless Green Parentheses Sleep Furiously
|
ユーザー |
|
提出日時 | 2025-05-17 01:37:34 |
言語 | Rust (1.83.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,253 bytes |
コンパイル時間 | 12,448 ms |
コンパイル使用メモリ | 397,808 KB |
実行使用メモリ | 7,848 KB |
最終ジャッジ日時 | 2025-05-17 01:37:55 |
合計ジャッジ時間 | 19,243 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 WA * 2 |
other | AC * 18 WA * 31 |
ソースコード
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; ans.push('('); stack.push(0); } ')' => { let i = stack.pop().unwrap(); match i { 0 => ans.push_str("1+1"), 1 => ans.push_str("+1"), _ => (), } ans.push(')'); } _ => unreachable!(), } (stack.len() >= 1).then_some(())?; ans.push(c); } (stack.len() == 1).then_some(())?; let rest = k.checked_sub(ans.chars().filter(|&c| c == '1').count())?; for _ in 0..rest { ans.push_str("+1"); } Some(ans) }