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)

use std::cmp::{max, min};
use std::io::{stdin, Read};

macro_rules! parse {
    ($it: ident) => {
        $it.next().unwrap().parse().unwrap()
    };
    ($it: ident,2) => {
        (parse!($it), parse!($it))
    };
    ($it: ident,3) => {
        (parse!($it), parse!($it), parse!($it))
    };
    ($it: ident,4) => {
        (parse!($it), parse!($it), parse!($it), parse!($it))
    };
    ($it: ident, $n: expr) => {
        (&mut $it).take($n).map(|e| e.parse().unwrap()).collect();
    };
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    let mut it = s.split_whitespace();
    let a = it.next().unwrap();
    let b = it.next().unwrap();
    let c = it.next().unwrap();
    let equal = |c, d| c == d || c == '?' || d == '?';
    let cmp = |a: &str, b: &str| a.chars().zip(b.chars()).all(|(c, d)| equal(c, d));
    let M = 2000isize;
    let calc_a2b = |a: &str, b: &str| {
        let mut ab = vec![true; (2 * M + 1) as usize];
        for i in (-M)..(M + 1) {
            let ai = max(0, i) as usize;
            let bi = max(0, -i) as usize;
            if ai >= a.len() || bi >= b.len() {
                continue;
            }
            ab[(i + M) as usize] = cmp(&a[ai..], &b[bi..]);
        }
        ab
    };
    let ab = calc_a2b(a, b);
    let bc = calc_a2b(b, c);
    let ac = calc_a2b(a, c);
    let check = |ab: &Vec<bool>, i: isize| {
        if i < ab.len() as isize && i >= 0 {
            ab[i as usize]
        } else {
            true
        }
    };
    let M = 2000isize;
    let mut ret = (M * 3) as usize;
    for i in (-M * 2)..(M * 2 + 1) {
        for j in (-M * 2)..(M * 2 + 1) {
            if check(&ab, i + M) && check(&bc, j - i + M) && check(&ac, j + M) {
                let l = min(i, j);
                let l = min(l, 0);
                let r = max(a.len() as isize, i + b.len() as isize);
                let r = max(r, j + c.len() as isize);
                ret = min(ret, (r - l) as usize);
            }
        }
    }
    println!("{:?}", ret); // DEBUG
}