AtCoder Beginner Contest 261 C

https://atcoder.jp/contests/abc261/tasks/abc261_c

結局、HashMapはentryを使わないとうまくいかないですね。

// NewFolder(1)
#![allow(non_snake_case)]

use std::collections::HashMap;

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

//////////////////// process ////////////////////

fn main() {
    let N: usize = read();
    let S: Vec<String> = (0..N).map(|_| read()).collect();
    let mut m = HashMap::<String, usize>::new();
    for s in S.iter() {
        let s1 = match m.get(s) {
            Some(f) => format!("{}({})", s, f),
            None    => s.to_string()
        };
        println!("{}", s1);
        let counter = m.entry(s.to_string()).or_insert(0);
        *counter += 1;
    }
}