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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;

public class Main {
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		char a[] = scan.nextLine().toCharArray();
		char b[] = scan.nextLine().toCharArray();
		char c[] = scan.nextLine().toCharArray();
		
		int max = Integer.MAX_VALUE;
		max = Math.min(max, compose(compose(a, b), c).length);
		max = Math.min(max, compose(compose(b, a), c).length);
		max = Math.min(max, compose(compose(a, c), b).length);
		max = Math.min(max, compose(compose(c, a), b).length);
		max = Math.min(max, compose(compose(b, c), a).length);
		max = Math.min(max, compose(compose(c, b), a).length);
		max = Math.min(max, compose(a, compose(b, c)).length);
		max = Math.min(max, compose(a, compose(c, b)).length);
		max = Math.min(max, compose(b, compose(a, c)).length);
		max = Math.min(max, compose(b, compose(c, a)).length);
		max = Math.min(max, compose(c, compose(a, b)).length);
		max = Math.min(max, compose(c, compose(b, a)).length);
		System.out.println(max);
	}
	
	public static char[] compose(char[] a, char[] b) {
		int A = a.length;
		int B = b.length;
		
		int length = 0;
		
		for(int i = 0;i < Math.min(A, B);i ++) {
			for(int j = 0;j < i + 1;j ++) {
				if(!equal(a[A - i - 1 + j], b[j])) break;
				if(j == i) length = Math.max(length, j + 1);
			}
		}
		
		char[] ans = new char[A + B - length];
		for(int i = 0;i < A - length;i ++) {
			ans[i] = a[i];
		}
		for(int i = 0;i < length;i ++) {
			ans[A - length + i] = check(a[A - length + i], b[i]);
		}
		for(int i = 0;i < B - length;i ++) {
			ans[A + i] = b[length + i];
		}
		return ans;
	}
	
	public static char check(char a, char b) {
		if(a == '?') return b;
		return a;
	}
	
	public static boolean equal(char a, char b) {
		return a == b || a == '?' || b == '?';
	}
}