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;

const N: usize = 4000;
const M: usize = 2 * N + 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 = cross(&a, &b);
    let bc = cross(&b, &c);
    let ac = cross(&a, &c);

    let mut ans = a.len() + b.len() + c.len();
    let a_pos = N;
    for b_pos in 0..M {
        if !ab[b_pos] {
            continue;
        }
        for c_pos in 0..M {
            if !ac[c_pos] {
                continue;
            }

            let head = vec![a_pos, b_pos, c_pos].into_iter().min().unwrap();
            let tail = vec![a_pos + a.len(), b_pos + b.len(), c_pos + c.len()]
                .into_iter()
                .max()
                .unwrap();
            if b_pos >= N {
                if c_pos < b_pos - N || bc[c_pos - (b_pos - N)] {
                    ans = cmp::min(ans, tail - head);
                }
            } else {
                if c_pos + N - b_pos >= M || bc[c_pos + N - b_pos] {
                    ans = cmp::min(ans, tail - head);
                }
            }
        }
    }

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

fn cross(a: &Vec<char>, b: &Vec<char>) -> [bool; M] {
    let mut ab = [true; M];
    let a_pos = N;
    for b_pos in 0..(M - b.len()) {
        let mut s = ['?'; M];
        for (i, &a) in a.iter().enumerate() {
            s[i + a_pos] = a;
        }

        let mut ok = true;
        for (i, &b) in b.iter().enumerate() {
            if s[i + b_pos] != '?' && s[i + b_pos] != b && b != '?' {
                ok = false;
                break;
            }
        }
        ab[b_pos] = ok;
    }
    ab
}

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: std::ops::Deref<Target = str>>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.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()
    }
}