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: Accepted)

fn read_stdin() -> String {
    use std::io::Read;
    let mut input = String::new();
    std::io::stdin().read_to_string(&mut input).unwrap();
    input
}

fn scan<'a, I: Iterator<Item = &'a str>, T: std::str::FromStr>(tokens: &mut I) -> T {
    tokens.next().unwrap().parse().ok().unwrap()
}

fn vec<T, F: FnMut(usize) -> T>(n: usize, mut f: F) -> Vec<T> {
    (0..n).map(|i| f(i)).collect()
}

fn main() {
    let input = read_stdin();
    let tokens = &mut input.split_whitespace();
    let a: String = scan(tokens);
    let b: String = scan(tokens);
    let c: String = scan(tokens);
    let (a, b, c) = (a.as_bytes(), b.as_bytes(), c.as_bytes());
    let ab = &fit_array(a, b);
    let ac = &fit_array(a, c);
    let ba = &fit_array(b, a);
    let bc = &fit_array(b, c);
    let ca = &fit_array(c, a);
    let cb = &fit_array(c, b);
    let mut min = a.len() + b.len() + c.len();
    min = std::cmp::min(min, align(a, b, c, ab, ac, bc));
    min = std::cmp::min(min, align(a, c, b, ac, ab, cb));
    min = std::cmp::min(min, align(b, a, c, ba, bc, ac));
    min = std::cmp::min(min, align(b, c, a, bc, ba, ca));
    min = std::cmp::min(min, align(c, a, b, ca, cb, ab));
    min = std::cmp::min(min, align(c, b, a, cb, ca, ba));
    println!("{}", min);
}

fn align(a: &[u8], b: &[u8], c: &[u8], ab: &[bool], ac: &[bool], bc: &[bool]) -> usize {
    let la = a.len();
    let lb = b.len();
    let lc = c.len();
    let mut min = la + lb + lc;
    for i in 0..la + 1 {
        if i == la || ab[i] {
            for j in i..std::cmp::max(la, i + lb) + 1 {
                if (j >= la || ac[j]) && (j - i >= lb || bc[j - i]) {
                    min = std::cmp::min(min, std::cmp::max(la, std::cmp::max(i + lb, j + lc)));
                }
            }
        }
    }
    min
}

fn fit_array(a: &[u8], b: &[u8]) -> Vec<bool> {
    vec(a.len(), |i| {
        let l = std::cmp::min(a.len() - i, b.len());
        fit(&a[i..i + l], &b[..l])
    })
}

fn fit(a: &[u8], b: &[u8]) -> bool {
    for i in 0..a.len() {
        if a[i] != b'?' && b[i] != b'?' && a[i] != b[i] {
            return false;
        }
    }
    return true;
}