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)

macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut $ iter = s . split_whitespace ( ) ; input_inner ! { $ iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ r ) * } } ; }
macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , { chars : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
use std::cmp::{max, min};
fn same(x: char, y: char) -> bool {
    x == y || x == '?' || y == '?'
}
fn solve(a: &Vec<char>, b: &Vec<char>, c: &Vec<char>) -> usize {
    let (n, m, l) = (a.len(), b.len(), c.len());
    let mut ans = n + m + l;
    let mut ab = vec![true; n + 1];
    let mut ac = vec![true; n + 1];
    let mut bc = vec![true; m + 1];
    for i in 0..n + 1 {
        for j in 0..m {
            if i + j == n {
                break;
            }
            if !same(a[i + j], b[j]) {
                ab[i] = false;
                break;
            }
        }
    }
    for i in 0..n + 1 {
        for j in 0..l {
            if i + j == n {
                break;
            }
            if !same(a[i + j], c[j]) {
                ac[i] = false;
                break;
            }
        }
    }
    for i in 0..m + 1 {
        for j in 0..l {
            if i + j == m {
                break;
            }
            if !same(b[i + j], c[j]) {
                bc[i] = false;
            }
        }
    }
    for i in 0..n + 1 {
        for j in i..max(i + m, n) + 1 {
            if ab[i] && (j >= n || ac[j]) && (j - i >= m || bc[j - i]) {
                ans = min(ans, max(a.len(), max(i + b.len(), j + c.len())));
            }
        }
    }
    ans
}
fn main() {
    input! { a: chars, b: chars, c: chars };
    let mut ans = a.len() + b.len() + c.len();
    ans = min(ans, solve(&a, &b, &c));
    ans = min(ans, solve(&a, &c, &b));
    ans = min(ans, solve(&b, &a, &c));
    ans = min(ans, solve(&b, &c, &a));
    ans = min(ans, solve(&c, &a, &b));
    ans = min(ans, solve(&c, &b, &a));
    println!("{}", ans);
}