結果

問題 No.3685 ワロングアンサーやんけ!
コンテスト
ユーザー norioc
提出日時 2026-09-05 17:36:02
言語 Rust
(1.97.1 + proconio + num + itertools)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
AC  
実行時間 156 ms / 2,000 ms
+ 513µs
コード長 2,431 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,657 ms
コンパイル使用メモリ 189,236 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2026-09-05 17:36:30
合計ジャッジ時間 7,130 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#![allow(non_snake_case, unused_imports)]

use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap, HashSet};
use proconio::{input, marker::Usize1, marker::Chars};
use itertools::Itertools;

#[allow(unused_macros)]
macro_rules! d {
    ( $( $x:expr ),* $(,)? ) => {
        eprintln!(
            concat!( $( stringify!($x), "={:?} " ),* ),
            $( $x ),*
        );
    };
}

#[allow(dead_code)]
fn yn(b: bool) -> &'static str {
    if b { "Yes" } else { "No" }
}

fn replace_first(s: &str, fm: char, to: &str) -> String {
    let mut res = String::new();

    let mut replaced = false;
    for c in s.chars() {
        if !replaced && c == fm {
            res.push_str(to);
            replaced = true;
        } else {
            res.push(c)
        }
    }
    res
}

fn solve() -> String {
    input! {
        R: String,
        S: String,
        K: usize,
    }

    let prefix_count = |c: char| -> usize {
        R.chars()
            .take(K)
            .filter(|&x| x == c)
            .count()
    };

    let suffix_count = |c: char| -> usize {
        R.chars()
            .skip(K)
            .filter(|&x| x == c)
            .count()
    };

    match S.as_str() {
        "Warong" => {
            let mut res = String::new();
            for _ in 0..K {
                res.push('A');
            }

            let q = suffix_count('?');
            let w = suffix_count('W');
            let cs: Vec<char> = R.chars().collect();
            let mut b = q == 1 && w == 0; // ? を W に置き換える
            for i in K as usize..R.len() {
                if cs[i] == '?' && b {
                    res.push('W');
                    b = false;
                } else {
                    res.push(cs[i]);
                }
            }
            res
        }

        "NotWarong" => {
            if prefix_count('A') == K {
                return R.replace('?', "A");
            }

            let suffix_has_w = suffix_count('W') > 0;
            if suffix_has_w {
                let a = prefix_count('A');
                let q = prefix_count('?');
                if a == K-1 && q == 1 {
                    return replace_first(&R, '?', "W");
                }
            }
            R
        }

        _ => unreachable!(),
    }
}

fn main() {
    input! {
        T: usize,
    }

    for _ in 0..T {
        let ans = solve();
        println!("{}", ans);
    }
}
0