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;
use std::cmp::{max, min};
use std::collections::{BTreeMap, BTreeSet, VecDeque};

const W: usize = 4001;
const N: usize = W * 2;

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 = construct(&a, &b);
    let ac = construct(&a, &c);
    let bc = construct(&b, &c);

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

            let head = min(min(b_start, c_start), W);
            let tail = max(max(b_start + b.len(), c_start + c.len()), W + a.len());

            // b= b_start..(b_start+b.len())
            if b_start >= W && c_start >= b_start - W {
                let c = c_start - (b_start - W);
                if bc[c] {
                    ans = min(ans, tail - head);
                }
            } else if b_start < W && c_start + W - b_start < N {
                let c = c_start + W - b_start;
                if bc[c] {
                    ans = min(ans, tail - head);
                }
            }
        }
    }

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

fn construct(a: &Vec<char>, b: &Vec<char>) -> Vec<bool> {
    let mut row = vec!['?'; N];
    for i in 0..a.len() {
        row[i + W] = a[i];
    }

    let mut ok = vec![true; N];
    for start in 0..(N - b.len() + 1) {
        for i in 0..b.len() {
            if row[start + i] != '?' && row[start + i] != b[i] && b[i] != '?' {
                ok[start] = false;
                break;
            }
        }
    }
    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) -> Self {
        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()
    }
}