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)

#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

fn get(a: &Vec<char>, i: usize) -> char {
    if i < a.len() {
        a[i]
    } else {
        '?'
    }
}

fn merge(a: char, b: char) -> char {
    if a == '?' {
        b
    } else if b == '?' {
        a
    } else {
        a
    }
}

fn eq(a: char, b: char) -> bool {
    a == '?' || b == '?' || a == b
}

fn minsup(a: &Vec<char>, b: &Vec<char>) -> Vec<char> {
    for offset in 0..a.len() + 1 {
        let mut ok = true;
        for i in 0..b.len() {
            if eq(get(&a, offset + i), get(&b, i)) {
                continue
            } else {
                ok = false;
                break
            }
        }
        if ok {
            let mut r = vec![];
            for i in 0..offset {
                r.push(a[i]);
            }
            for i in offset..max!(a.len(), b.len() + offset) {
                let c = get(a, i);
                let d = get(b, i - offset);
                r.push(merge(c, d));
            }
            return r
        }
    }
    vec![]
}

fn main() {

    let mut sc = Scanner::new();
    let a: Vec<char> = sc.chars();
    let b: Vec<char> = sc.chars();
    let c: Vec<char> = sc.chars();

    let ab = minsup(&a, &b);
    let ba = minsup(&b, &a);

    let ac = minsup(&a, &c);
    let ca = minsup(&c, &a);

    let bc = minsup(&b, &c);
    let cb = minsup(&c, &b);

    let ab_c = minsup(&ab, &c);
    let c_ab = minsup(&c, &ab);
    let ba_c = minsup(&ba, &c);
    let c_ba = minsup(&c, &ba);

    let ac_b = minsup(&ac, &b);
    let b_ac = minsup(&b, &ac);
    let ca_b = minsup(&ca, &b);
    let b_ca = minsup(&b, &ca);

    let bc_a = minsup(&bc, &a);
    let a_bc = minsup(&a, &bc);
    let cb_a = minsup(&cb, &a);
    let a_cb = minsup(&a, &cb);

    let lens = vec![
        ab_c.len(),
        c_ab.len(),
        ba_c.len(),
        c_ba.len(),
        ac_b.len(),
        b_ac.len(),
        ca_b.len(),
        b_ca.len(),
        bc_a.len(),
        a_bc.len(),
        cb_a.len(),
        a_cb.len(),
    ];
    put!(min!(..lens).unwrap());
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> { self.cin::<String>().chars().collect() }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.cin()).collect() }
}