Problem:
Score : 100 points

Problem Statement

You will be given a string S of length 3 representing the weather forecast for three days in the past.

The i-th character (1 \leq i \leq 3) of S represents the forecast for the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

You will also be given a string T of length 3 representing the actual weather on those three days.

The i-th character (1 \leq i \leq 3) of S represents the actual weather on the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

Print the number of days for which the forecast was correct.

Constraints

S and T are strings of length 3 each.

S and T consist of S, C, and R.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the number of days for which the forecast was correct.

Sample Input 1

CSS
CSR

Sample Output 1

2

For the first day, it was forecast to be cloudy, and it was indeed cloudy.

For the second day, it was forecast to be sunny, and it was indeed sunny.

For the third day, it was forecast to be sunny, but it was rainy.

Thus, the forecast was correct for two days in this case.

Sample Input 2

SSR
SSR

Sample Output 2

3

Sample Input 3

RRR
SSS

Sample Output 3

0

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

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

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

type Game = (usize, usize, usize); // p1, p2, id
type IdandTrace = (usize, bool); // id, forFinished

fn main() {
    let n = read::<usize>();
    let m = n * (n - 1); // Num of Games

    let mut initial_games: Vec<Game> = Vec::with_capacity(n);
    let mut graph: Vec<Vec<Game>> = vec![Vec::with_capacity(m); m];
    for i in 0..n {
        let row = read_vec::<usize>();
        if row.len() <= 0 {
            continue;
        }
        let mut g_prev = get_game(n, i, row[0]);
        initial_games.push(g_prev);
        for j in 1..row.len() {
            let g = get_game(n, i, row[j]);
            graph[g_prev.2].push(g);
            g_prev = g;
        }
    }

    let d = solve(n, m, &graph, &initial_games);
    println!("{}", d);
}

fn get_game(n: usize, i: usize, j: usize) -> Game {
    let mut p1 = i;
    let mut p2 = j - 1;
    if p1 > p2 {
        let tmp = p1;
        p1 = p2;
        p2 = tmp;
    }
    return (p1, p2, p1 * n + p2);
}

fn solve(n: usize, m: usize, graph: &[Vec<Game>], initial_games: &[Game]) -> i32 {
    //println!("XXX 000 {}, {}", n, m);
    let mut lp:Vec<(bool, i32)> = vec![(false, 0); m];
    let mut max: i32 = -1;
    for g in initial_games {
        //println!("XXX g = {:?}", g);
        if lp[g.2].0 {
            // Already tried.
            continue;
        }
        let d = dfs(n, m, graph, g, &lp);
        //println!("XXX d = {} --> {}", d, g.2);
        lp[g.2] = (true, d);
        if d < 0 {
            return d;
        }
        if d > max {
            max = d;
        }
    }
    // Num of nodes = distance + 1
    return max + 1;
}

/**
 * Calculate the length of the longest path by DFS.
 * Returns -1 if it contains the cycle.
 */
fn dfs(n: usize, m: usize, graph: &[Vec<Game>], initial_game: &Game, lp: &[(bool, i32)]) -> i32 {
    let mut visited: Vec<bool> = vec![false; m];
    let mut finished: Vec<bool> = vec![false; m];

    let mut d: Vec<i32> = vec![0; m];
    let mut stack: Vec<IdandTrace> = Vec::with_capacity(m);

    stack.push((initial_game.2, false));
    let mut max = 0;
    while !stack.is_empty() {
        let iat = stack.pop().unwrap();

        // A. Fin entry
        if iat.1 {
            finished[iat.0] = true;
            continue;
        }

        // B. Normal entry
        visited[iat.0] = true;
        stack.push((iat.0, true));

        // B-1 No child
        if graph[iat.0].len() == 0 {
            continue;
        }

        // B-3 Some children
        let new_d = d[iat.0] + 1;
        for next in &graph[iat.0] {
            //println!("YYY {:?}", next);
            if visited[next.2] && !finished[next.2] {
                // Cycle!
                //println!("YYY Cycle!");
                return -1;
            }

            if lp[next.2 % n].0 {
                //println!("YYY Already tried: {:?}", lp[next.2]);
                // Already tried.
                if lp[next.2].1 < 0 {
                    return -1;
                }
                return new_d + lp[next.2].1;
            }


            if d[next.2] < new_d {
                // Update
                d[next.2] = new_d;
                stack.push((next.2, false));
                if new_d > max {
                    max = new_d;
                }
            }
        }
    }
    return max;
}