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)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;
use proconio::marker::Bytes;

fn match_char(c1: u8, c2: u8) -> bool {
    c1 == b'?' || c2 == b'?' || c1 == c2
}

fn main() {
    input! {
        a: Bytes,
        b: Bytes,
        c: Bytes,
    };

    let mut ans = a.len() + b.len() + c.len();
    ans = std::cmp::min(ans, f(&a, &b, &c));
    ans = std::cmp::min(ans, f(&a, &c, &b));
    ans = std::cmp::min(ans, f(&b, &a, &c));
    ans = std::cmp::min(ans, f(&b, &c, &a));
    ans = std::cmp::min(ans, f(&c, &a, &b));
    ans = std::cmp::min(ans, f(&c, &b, &a));
    println!("{}", ans);
}

fn f(av: &Vec<u8>, bv: &Vec<u8>, cv: &Vec<u8>) -> usize {
    use std::cmp::{max, min};

    let oabv = new_ov(&av, &bv);
    let obcv = new_ov(&bv, &cv);
    let oacv = new_ov(&av, &cv);

    let mut len = av.len() + bv.len() + cv.len();
    for ob in oabv
        .iter()
        .enumerate()
        .filter(|(_, &ok)| ok)
        .map(|(i, _)| i)
    {
        let r = max(av.len(), ob + bv.len());
        for oc in ob..=r {
            if (oc - ob >= obcv.len() || obcv[oc - ob]) && (oc >= oacv.len() || oacv[oc]) {
                len = min(len, max(r, oc + cv.len()));
            }
        }
    }
    len
}

fn new_ov(av: &Vec<u8>, bv: &Vec<u8>) -> Vec<bool> {
    let mut ov = vec![false; av.len() + 1];
    for i_okv in 0..ov.len() {
        let mut ok = true;
        for i_av in i_okv..std::cmp::min(av.len(), i_okv + bv.len()) {
            if !match_char(av[i_av], bv[i_av - i_okv]) {
                ok = false;
                break;
            }
        }
        ov[i_okv] = ok;
    }
    ov
}
*/

fn main() {
    let exe = "/tmp/bin141901F1";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";