Problem:
Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.

Solution in Rust (1.42.0) (judge verdict: Accepted)

use proconio::input;
use std::collections::HashMap;

#[derive(Hash, Eq, PartialEq, Ord, PartialOrd, Clone, Copy)]
struct State {
    is_accept: bool,
    id: usize,
}

struct Automaton {
    states: HashMap<String, State>,
    transitions: HashMap<State, Box<dyn Fn(char) -> String>>,
    id: usize,
}

impl Automaton {
    fn add_states(&mut self, name: &str, is_accept: bool, transition: fn(char) -> String) {
        let state = State {
            is_accept,
            id: self.id,
        };
        self.id += 1;
        self.states.insert(name.to_string(), state);
        self.transitions.insert(state, Box::new(transition));
    }
    fn run(&self, s: String) -> bool {
        let mut current_state = *self.states.get("q0").unwrap();
        for c in s.chars() {
            let transition = self.transitions.get(&current_state).unwrap();
            let next_state_name = transition(c);
            current_state = *self.states.get(&next_state_name).unwrap();
        }
        current_state.is_accept
    }
}

fn transition_q0(c: char) -> String {
    if c == 'A' {
        return "q2".to_string();
    }

    "q1".to_string()
}
fn transition_q1(c: char) -> String {
    if c == 'A' {
        return "q3".to_string();
    }

    "q1".to_string()
}
fn transition_q2(c: char) -> String {
    if c == 'A' {
        return "q2".to_string();
    }

    "q3".to_string()
}
fn transition_q3(_c: char) -> String {
    "q3".to_string()
}

fn main() {
    input! {
        s: String,
    }

    let mut m = Automaton {
        states: HashMap::new(),
        transitions: HashMap::new(),
        id: 0,
    };

    m.add_states("q0", false, transition_q0);
    m.add_states("q1", false, transition_q1);
    m.add_states("q2", false, transition_q2);
    m.add_states("q3", true, transition_q3);

    if m.run(s) {
        println!("Yes");
    } else {
        println!("No");
    }
}