Problem:
Score : 500 points

Problem Statement

Snuke has a string s.
From this string, Anuke, Bnuke, and Cnuke obtained strings a, b, and c, respectively, as follows:

Choose a non-empty (contiguous) substring of s (possibly s itself). Then, replace some characters (possibly all or none) in it with ?s.

For example, if s is mississippi, we can choose the substring ssissip and replace its 1-st and 3-rd characters with ? to obtain ?s?ssip.

You are given the strings a, b, and c.
Find the minimum possible length of s.

Constraints

1 \leq |a|, |b|, |c| \leq 2000

a, b, and c consists of lowercase English letters and ?s.

Input

Input is given from Standard Input in the following format:

a
b
c

Output

Print the minimum possible length of s.

Sample Input 1

a?c
der
cod

Sample Output 1

7

For example, s could be atcoder.

Sample Input 2

atcoder
atcoder
???????

Sample Output 2

7

a, b, and c may not be distinct.

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

fn main() {
    let a = read::<String>().chars().collect::<Vec<_>>();
    let b = read::<String>().chars().collect::<Vec<_>>();
    let c = read::<String>().chars().collect::<Vec<_>>();
    let vv: Vec<Vec<Vec<char>>> = vec![
        vec![a.clone(), b.clone(), c.clone()],
        vec![a.clone(), c.clone(), b.clone()],
        vec![b.clone(), a.clone(), c.clone()],
        vec![b.clone(), c.clone(), a.clone()],
        vec![c.clone(), a.clone(), b.clone()],
        vec![c.clone(), b.clone(), a.clone()],
    ];

    let max_len = a.len() + b.len() + c.len();
    let mut ans = max_len;
    for v in vv {
        //println!("{:?}", v);
        let mut len = v[0].len();
        let mut ns = vec![];

        let mut ok1 = false;
        for i in 0..v[0].len() {
            let minlen = std::cmp::min(v[0].len() - i, v[1].len());
            let mut f = true;
            for j in 0..minlen {
                //println!("minlen: {}, i:{}, j:{}", minlen, i, j);
                if v[0][i + j] == v[1][j] || v[0][i + j] == '?' || v[1][j] == '?' {
                    continue;
                } else {
                    f = false;
                    break;
                }
            }
            if f {
                len = std::cmp::max(v[0].len(), i + v[1].len());
                for k in 0..len {
                    let l0 = v[0].len();
                    let l1 = v[1].len();

                    if k < i {
                        ns.push(v[0][k]);
                    } else {
                        if k < l0 && i + k < l1 {
                            if v[0][k] != '?' {
                                ns.push(v[0][k]);
                            } else {
                                ns.push(v[1][k - i]);
                            }
                        } else if k < l0 {
                            ns.push(v[0][k]);
                        } else {
                            ns.push(v[1][k - i]);
                        }
                    }
                    //println!("{:?}", ns);
                }
                ok1 = true;
                break;
            }
        }
        if !ok1 {
            let mut a = v[0].clone();
            let mut b = v[1].clone();
            ns.append(&mut a);
            ns.append(&mut b);
            len = ns.len();
        }

        //println!("{:?}", ns);

        let mut ok2 = false;
        for i in 0..ns.len() {
            //println!("i: {}", i);
            let minlen = std::cmp::min(ns.len() - i, v[2].len());
            let mut f = true;
            for j in 0..minlen {
                if ns[i + j] == v[2][j] || ns[i + j] == '?' || v[2][j] == '?' {
                    continue;
                } else {
                    f = false;
                    break;
                }
            }
            if f {
                len = std::cmp::max(ns.len(), i + v[2].len());
                ok2 = true;
                break;
            }
        }
        if !ok2 {
            let mut c = v[2].clone();
            ns.append(&mut c);
            len = ns.len();
        }

        //println!("{}", len);
        ans = std::cmp::min(ans, len);
    }
    println!("{}", ans);
}

#[allow(dead_code)]
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()
}
#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
#[allow(dead_code)]
fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}
#[allow(dead_code)]
fn yn(result: bool) {
    if result {
        println!("Yes");
    } else {
        println!("No");
    }
}