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)

use std::cmp;

const N: usize = 2000;
const W: usize = N * 2 + 1;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let a = sc.chars();
    let b = sc.chars();
    let c = sc.chars();

    let ab = calc(&a, &b);
    let bc = calc(&b, &c);
    let ac = calc(&a, &c);

    let mut ans = a.len() + b.len() + c.len();
    for b_start in 0..W {
        for c_start in 0..W {
            if !ab[b_start] || !ac[c_start] {
                continue;
            }

            let min = cmp::min(cmp::min(b_start, c_start), N);
            let max = cmp::max(cmp::max(b_start + b.len(), c_start + c.len()), N + a.len());
            let length = max - min;

            if b_start < N {
                let right = N - b_start;
                let c_start = c_start + right;
                if bc.len() <= c_start || bc[c_start] {
                    ans = cmp::min(ans, length);
                }
            } else {
                let left = b_start - N;
                if c_start < left || bc[c_start - left] {
                    ans = cmp::min(ans, length);
                }
            }
        }
    }

    println!("{}", ans);
}

fn calc(a: &Vec<char>, b: &Vec<char>) -> [bool; W] {
    let mut is_ok = [false; W];

    let mut s = ['?'; W];
    for i in 0..a.len() {
        s[i + N] = a[i];
    }

    for b_start in 0..W {
        let mut ok = true;
        for i in 0..b.len() {
            if i + b_start < W {
                if s[i + b_start] != '?' && s[i + b_start] != b[i] && b[i] != '?' {
                    ok = false;
                    break;
                }
            }
        }
        is_ok[b_start] = ok;
    }
    is_ok
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}