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: Runtime Error)

#[allow(unused_imports)]
use std::char::from_digit;
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::fs::File;
#[allow(unused_imports)]
use std::io::prelude::*;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufReader, BufWriter, Stdin, Stdout, Write};
#[allow(unused_imports)]
use std::mem::swap;
#[allow(unused_imports)]
use std::str::FromStr;

type BoxResult<T> = Result<T, Box<std::error::Error>>;


fn main() {
    let mut scanner = Scanner::new(stdin());
    let a = scanner.read_chars().unwrap();
    let b = scanner.read_chars().unwrap();
    let c = scanner.read_chars().unwrap();
    let ans = solve(&a, &b, &c);
    println!("{}", ans);
}

fn solve(a: &[char], b: &[char], c: &[char]) -> usize {
    let b_against_a = calc_matching(a, b);
    let c_against_a = calc_matching(a, c);
    let c_against_b = calc_matching(b, c);
    println!("{:?}", b_against_a);
    println!("{:?}", c_against_a);
    println!("{:?}", c_against_b);


    let mut minimum = std::usize::MAX;
    let offset = b.len() + c.len();
    for i in 0..(a.len() + b.len() + 2 * c.len() + 1) {
        if i < offset && offset - i < b.len() && !b_against_a[i + b.len() - offset] {
            continue;
        }
        if i >= offset && i < offset + a.len() && !b_against_a[i + b.len() - offset] {
            continue;
        }
        for j in 0..(a.len() + 2 * b.len() + c.len() + 1) {
            if j < offset && offset - j < c.len() && !c_against_a[j + c.len() - offset] {
                continue;
            }
            if j >= offset && j < offset + a.len() && !c_against_a[j + c.len() - offset] {
                continue;
            }
            if j < i && i - j < c.len() && !c_against_b[c.len() + j - i] {
                continue;
            }
            if j >= i && j - i < a.len() && !c_against_b[c.len() + j - i] {
                continue;
            }
            let s = min(offset, min(i, j));
            let e = max(offset + a.len() - 1, max(i + b.len() - 1, j + c.len() - 1));
            let length = e - s + 1;
            minimum = min(length, minimum);
        }
    }
    minimum
}

fn calc_matching(a: &[char], b: &[char]) -> Vec<bool> {
    let mut matched = vec![true; a.len() + b.len()];
    for i in 1..b.len() {
        for j in 0..min(i, a.len()) {
            if a[j] == '?' || b[b.len() - i + j] == '?' {
                continue;
            }
            if a[j] != b[b.len() - i + j] {
                matched[i] = false;
                break;
            }
        }
    }
    let offset = b.len();
    for i in 0..a.len() {
        for j in i..min(a.len(), i + b.len()) {
            if a[j] == '?' || b[j - i] == '?' {
                continue;
            }
            if a[j] != b[j - i] {
                matched[offset + i] = false;
                break;
            }
        }
    }
    matched
}


#[allow(dead_code)]
struct Scanner<R> {
    reader: BufReader<R>,
    buffer: String,
}
impl<R> Scanner<R>
where
    R: Read,
{
    #[allow(dead_code)]
    fn new(inner: R) -> Scanner<R> {
        let reader = BufReader::new(inner);
        let buffer = String::new();
        Scanner {
            reader: reader,
            buffer: buffer,
        }
    }
    #[allow(dead_code)]
    fn with_capacity(capacity: usize, inner: R) -> Scanner<R> {
        let reader = BufReader::with_capacity(capacity, inner);
        let buffer = String::new();
        Scanner {
            reader: reader,
            buffer: buffer,
        }
    }
    #[allow(dead_code)]
    fn read_line(&mut self) -> BoxResult<String> {
        self.reader.read_line(&mut self.buffer)?;
        let cloned = self.buffer.trim().to_string();
        self.buffer.clear();
        Ok(cloned)
    }
    #[allow(dead_code)]
    fn read<T>(&mut self) -> BoxResult<T>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let parsed = self.buffer.trim().parse()?;
        self.buffer.clear();
        Ok(parsed)
    }
    #[allow(dead_code)]
    fn read_vector<T>(&mut self) -> BoxResult<Vec<T>>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let parsed_vec = self
            .buffer
            .trim()
            .split_whitespace()
            .map(|s| s.parse())
            .collect::<Result<Vec<_>, _>>()?;
        self.buffer.clear();
        Ok(parsed_vec)
    }
    #[allow(dead_code)]
    fn read_matrix<T>(&mut self, lines: usize) -> BoxResult<Vec<Vec<T>>>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        let mut parsed_matrix = vec![];
        for _ in 0..lines {
            self.reader.read_line(&mut self.buffer)?;
            parsed_matrix.push(
                self.buffer
                    .trim()
                    .split_whitespace()
                    .map(|s| s.parse())
                    .collect::<Result<Vec<_>, _>>()?,
            );
            self.buffer.clear();
        }
        Ok(parsed_matrix)
    }
    #[allow(dead_code)]
    fn read_chars(&mut self) -> BoxResult<Vec<char>> {
        self.reader.read_line(&mut self.buffer)?;
        let chars = self.buffer.trim().chars().collect::<Vec<_>>();
        self.buffer.clear();
        Ok(chars)
    }
}