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::io::Read;

const GETA: usize = 4000;

fn calc(a: &[char], b: &[char]) -> Vec<bool> {
    let mut res = vec![true; 2 * GETA];
    for (i, res) in res.iter_mut().enumerate() {
        let l = std::cmp::max(i, GETA);
        let r = std::cmp::min(i + b.len(), GETA + a.len());
        if l >= r {
            continue;
        }
        if i >= GETA {
            *res = a[(i - GETA)..]
                .iter()
                .zip(b.iter())
                .all(|(a, b)| *a == *b || *a == '?' || *b == '?');
        } else {
            *res = a.iter()
                .zip(b[(GETA - i)..].iter())
                .all(|(a, b)| *a == *b || *a == '?' || *b == '?');
        }
    }
    res
}

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let a: Vec<char> = it.next().unwrap().chars().collect();
    let b: Vec<char> = it.next().unwrap().chars().collect();
    let c: Vec<char> = it.next().unwrap().chars().collect();
    let ab = calc(&a, &b);
    let ac = calc(&a, &c);
    let bc = calc(&b, &c);
    let mut ans = 6000;
    for (i, _) in ab.iter().enumerate().filter(|p| *p.1) {
        for (j, _) in ac.iter().enumerate().filter(|p| *p.1) {
            if *bc.get(j - i + GETA).unwrap_or(&false) {
                use std::cmp::*;
                let l = min(GETA, min(i, j));
                let r = max(GETA + a.len(), max(i + b.len(), j + c.len()));
                ans = min(r - l, ans);
            }
        }
    }
    println!("{}", ans);
}

fn main() {
    run();
}