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::cmp::{max, min};

const BASE: i32 = 30000;

fn find_set(a: &Vec<char>, b: &Vec<char>) -> Vec<bool> {
    let mut vv = Vec::new();
    vv.resize((BASE*2) as usize, true);
    let a_len = a.len() as i32;
    let b_len = b.len() as i32;
    for i in (-b_len+1)..a_len {
        let mut ok = true;
        for j in max(0, i)..min(a_len, i + b_len) {
            if a[j as usize] != '?' && b[(j - i) as usize] != '?' && a[j as usize] != b[(j - i) as usize] {
                ok = false;
                break;
            }
        }
        if !ok {
            vv[(BASE + i) as usize] = false;
        } else {
            //println!("{}", i);
        }
    }
    vv
}

fn solve(a: String, b: String, c: String) -> i32 {
    let a = a.chars().collect::<Vec<_>>();
    let b = b.chars().collect::<Vec<_>>();
    let c = c.chars().collect::<Vec<_>>();
    let a_len = a.len() as i32;
    let b_len = b.len() as i32;
    let c_len = c.len() as i32;
    let set_ab = find_set(&a, &b);
    let set_bc = find_set(&b, &c);
    let set_ac = find_set(&a, &c);
    let mut ans = 1<<29;
    for i in (-b_len-c_len)..(a_len+c_len+1) {
        // i: a to b position
        if !set_ab[(BASE + i) as usize] {
            continue;
        }
        for j in (min(0, i)-c_len)..(max(a_len, i+b_len)+1) {
            // j: a to c position
            if set_ac[(BASE + j) as usize] && set_bc[(BASE + j - i) as usize] {
                ans = min(ans, max(max(a_len, i+b_len), j+c_len) - min(min(0, i), j));
            }
        }
    }
    ans
}

fn main() {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).unwrap();
    let a = line.trim().to_string();
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).unwrap();
    let b = line.trim().to_string();
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).unwrap();
    let c = line.trim().to_string();
    let r = solve(a, b, c);
    println!("{}", r);
}