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 C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static System.Math;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Runtime.CompilerServices;
using Library;

namespace Program
{
    public static class PANASONIC2020E
    {
        static public int numberOfRandomCases = 0;
        static public void MakeTestCase(List<string> _input, List<string> _output, ref Func<string[], bool> _outputChecker)
        {
        }
        static public void Solve()
        {
            var a = NS;
            var b = NS;
            var c = NS;
            Func<string, long[]> f = s => s.Select(e => e == '?' ? -1L : (long)(e - 'a')).ToArray();
            Func<string, string, string, long> fun = (x, y, z) =>
            {
                var ret = x.Length + y.Length + z.Length;
                var hatenay = Repeat(-1L, y.Length).ToArray();
                var hatenaz = Repeat(-1L, z.Length).ToArray();
                var str = new List<long>();
                var fy = f(y);
                var fx = f(x);
                str.AddRange(hatenay);
                str.AddRange(fx);
                str.AddRange(hatenay);
                foreach (var item in LIB_String.ShiftAnd(str.ToArray(), fy))
                {
                    var sz = item - fy.Length + Max(fx.Length - item + fy.Length, fy.Length);
                    if (item < fy.Length) sz = fy.Length - item + Max(fx.Length, item);
                    var str2 = Repeat(-2L, sz).ToArray();
                    if (item < fy.Length)
                    {
                        for (var i = 0; i < fy.Length; i++)
                        {
                            str2[i] = fy[i];
                        }
                        for (var i = 0; i < fx.Length; i++)
                        {
                            if (str2[i + y.Length - item] < 0) str2[i + y.Length - item] = fx[i];
                        }
                    }
                    else
                    {
                        for (var i = 0; i < fx.Length; i++)
                        {
                            str2[i] = fx[i];
                        }
                        for (var i = 0; i < fy.Length; i++)
                        {
                            if (str2[i + item - fy.Length] < 0) str2[i + item - fy.Length] = fy[i];
                        }
                    }
                    var input = new List<long>();
                    input.AddRange(hatenaz);
                    input.AddRange(str2);
                    input.AddRange(hatenaz);
                    foreach (var item2 in LIB_String.ShiftAnd(input.ToArray(), f(z)))
                    {
                        var sz2 = z.Length - item2 + Max(str2.Length, item2);
                        if (item2 > z.Length)
                        {
                            sz2 = item2 - z.Length + Max(str2.Length - item2 + z.Length, z.Length);
                        }
                        ret = Min(ret, sz2);
                    }
                }
                return ret;
            };
            var ans = fun(a, b, c);
            ans = Min(ans, fun(a, c, b));
            ans = Min(ans, fun(b, c, a));
            Console.WriteLine(ans);
        }
        class Printer : StreamWriter
        {
            public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
            public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { base.AutoFlush = false; }
            public Printer(Stream stream, Encoding encoding) : base(stream, encoding) { base.AutoFlush = false; }
        }
        static LIB_FastIO fastio = new LIB_FastIODebug();
        static public void Main(string[] args) { if (args.Length == 0) { fastio = new LIB_FastIO(); Console.SetOut(new Printer(Console.OpenStandardOutput())); } var t = new Thread(Solve, 134217728); t.Start(); t.Join(); Console.Out.Flush(); }
        static long NN => fastio.Long();
        static double ND => fastio.Double();
        static string NS => fastio.Scan();
        static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray();
        static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray();
        static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray();
        static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => xorshift);
        static long Count<T>(this IEnumerable<T> x, Func<T, bool> pred) => Enumerable.Count(x, pred);
        static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n);
        static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c);
        static IEnumerable<string> OrderBy(this IEnumerable<string> x) => x.OrderBy(e => e, StringComparer.OrdinalIgnoreCase);
        static IEnumerable<T> OrderBy<T>(this IEnumerable<T> x, Func<T, string> selector) => x.OrderBy(selector, StringComparer.OrdinalIgnoreCase);
        static IEnumerable<string> OrderByDescending(this IEnumerable<string> x) => x.OrderByDescending(e => e, StringComparer.OrdinalIgnoreCase);
        static IEnumerable<T> OrderByDescending<T>(this IEnumerable<T> x, Func<T, string> selector) => x.OrderByDescending(selector, StringComparer.OrdinalIgnoreCase);
        static uint xorshift { get { _xsi.MoveNext(); return _xsi.Current; } }
        static IEnumerator<uint> _xsi = _xsc();
        static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = (uint)(DateTime.Now.Ticks & 0xffffffff); while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } }
    }
}
namespace Library {
    class LIB_FastIO
    {
        public LIB_FastIO() { str = Console.OpenStandardInput(); }
        readonly Stream str;
        readonly byte[] buf = new byte[1024];
        int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        byte read()
        {
            if (isEof) throw new EndOfStreamException();
            if (ptr >= len)
            {
                ptr = 0;
                if ((len = str.Read(buf, 0, 1024)) <= 0)
                {
                    isEof = true;
                    return 0;
                }
            }
            return buf[ptr++];
        }
        char Char()
        {
            byte b = 0;
            do b = read();
            while (b < 33 || 126 < b);
            return (char)b;
        }
        virtual public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        virtual public long Long()
        {
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != '-' && (b < '0' || '9' < b));
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        virtual public double Double() { return double.Parse(Scan(), CultureInfo.InvariantCulture); }
    }
    class LIB_FastIODebug : LIB_FastIO
    {
        Queue<string> param = new Queue<string>();
        string NextString() { if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item); return param.Dequeue(); }
        public LIB_FastIODebug() { }
        public override string Scan() => NextString();
        public override long Long() => long.Parse(NextString());
        public override double Double() => double.Parse(NextString());
    }
    class LIB_Bitset : IEquatable<LIB_Bitset>
    {
        long n;
        ulong[] ary;
        static readonly ulong[] ceil;
        static LIB_Bitset()
        {
            ceil = new ulong[64];
            ceil[0] = 0xffffffffffffffff;
            ceil[1] = 1;
            for (var i = 2; i < 64; i++)
            {
                ceil[i] = (ceil[i - 1] << 1) | 1;
            }
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_Bitset(long size)
        {
            if (size <= 0) throw new Exception();
            n = size;
            ary = new ulong[((n - 1) >> 6) + 1];
        }
        public long Count => n;
        public long CountOne => ary.Sum(e => LIB_BitUtil.CountOne(e));
        public bool this[int idx]
        {
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            get { return LIB_BitUtil.IsSet(ary[idx >> 6], idx & 63); }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            set
            {
                if (value) ary[idx >> 6] |= LIB_BitUtil.BitMask[idx & 63];
                else ary[idx >> 6] &= ~LIB_BitUtil.BitMask[idx & 63];
            }
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public LIB_Bitset operator &(LIB_Bitset x, LIB_Bitset y)
        {
            if (x.n < y.n) { var t = x; x = y; y = t; }
            var ret = new LIB_Bitset(x.n);
            for (var i = 0; i < ret.ary.Length; i++) ret.ary[i] = x.ary[i];
            for (var i = 0; i < y.ary.Length; i++) ret.ary[i] &= y.ary[i];
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public LIB_Bitset operator |(LIB_Bitset x, LIB_Bitset y)
        {
            if (x.n < y.n) { var t = x; x = y; y = t; }
            var ret = new LIB_Bitset(x.n);
            for (var i = 0; i < ret.ary.Length; i++) ret.ary[i] = x.ary[i];
            for (var i = 0; i < y.ary.Length; i++) ret.ary[i] |= y.ary[i];
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public LIB_Bitset operator ^(LIB_Bitset x, LIB_Bitset y)
        {
            if (x.n < y.n) { var t = x; x = y; y = t; }
            var ret = new LIB_Bitset(x.n);
            for (var i = 0; i < ret.ary.Length; i++) ret.ary[i] = x.ary[i];
            for (var i = 0; i < y.ary.Length; i++) ret.ary[i] ^= y.ary[i];
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public LIB_Bitset operator &(LIB_Bitset x, long y)
        {
            var ret = new LIB_Bitset(x.n);
            for (var i = 0; i < ret.ary.Length; i++) ret.ary[i] = x.ary[i];
            ret.ary[0] &= (ulong)y;
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public LIB_Bitset operator |(LIB_Bitset x, long y)
        {
            var ret = new LIB_Bitset(x.n);
            for (var i = 0; i < ret.ary.Length; i++) ret.ary[i] = x.ary[i];
            ret.ary[0] |= (ulong)y;
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public LIB_Bitset operator ^(LIB_Bitset x, long y)
        {
            var ret = new LIB_Bitset(x.n);
            for (var i = 0; i < ret.ary.Length; i++) ret.ary[i] = x.ary[i];
            ret.ary[0] ^= (ulong)y;
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Flip()
        {
            for (var i = 0; i < ary.Length; i++) ary[i] = ~ary[i];
            ary[ary.Length - 1] &= ceil[n & 63];
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public LIB_Bitset operator ~(LIB_Bitset x)
        {
            var ret = new LIB_Bitset(x.n);
            for (var i = 0; i < ret.ary.Length; i++) ret.ary[i] = ~x.ary[i];
            ret.ary[ret.ary.Length - 1] &= ceil[ret.n & 63];
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void ShiftLeft(int num)
        {
            var moveCnt = num >> 6;
            var moveBit = num & 63;
            for (var i = ary.Length - 1; i >= moveCnt; i--)
            {
                ary[i] = ary[i - moveCnt] << moveBit;
                if (moveBit > 0 && i > moveCnt) ary[i] |= ary[i - moveCnt - 1] >> (64 - moveBit);
            }
            ary[ary.Length - 1] &= ceil[n & 63];
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public LIB_Bitset operator <<(LIB_Bitset x, int num)
        {
            var ret = new LIB_Bitset(x.n);
            var moveCnt = num >> 6;
            var moveBit = num & 63;
            for (var i = ret.ary.Length - 1; i >= moveCnt; i--)
            {
                ret.ary[i] = x.ary[i - moveCnt] << moveBit;
                if (moveBit > 0 && i > moveCnt) ret.ary[i] |= x.ary[i - moveCnt - 1] >> (64 - moveBit);
            }
            ret.ary[ret.ary.Length - 1] &= ceil[ret.n & 63];
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void ShiftRight(int num)
        {
            var moveCnt = num >> 6;
            var moveBit = num & 63;
            var aryMax = ary.Length - moveCnt;
            for (var i = 0; i < aryMax; i++)
            {
                ary[i] = ary[i + moveCnt] >> moveBit;
                if (moveBit > 0 && i < aryMax - 1) ary[i] |= ary[i + moveCnt + 1] << (64 - moveBit);
            }
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public LIB_Bitset operator >>(LIB_Bitset x, int num)
        {
            var ret = new LIB_Bitset(x.n);
            var moveCnt = num >> 6;
            var moveBit = num & 63;
            var aryMax = ret.ary.Length - moveCnt;
            for (var i = 0; i < aryMax; i++)
            {
                ret.ary[i] = x.ary[i + moveCnt] >> moveBit;
                if (moveBit > 0 && i < aryMax - 1) ret.ary[i] |= x.ary[i + moveCnt + 1] << (64 - moveBit);
            }
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public bool operator ==(LIB_Bitset x, LIB_Bitset y)
        {
            if (x.n < y.n) { var t = x; x = y; y = t; }
            var i = 0;
            for (; i < y.ary.Length; i++) if (x.ary[i] != y.ary[i]) return false;
            for (; i < x.ary.Length; i++) if (x.ary[i] != 0) return false;
            return true;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public bool operator !=(LIB_Bitset x, LIB_Bitset y) => !(x == y);
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool Equals(LIB_Bitset x) => x == this;
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public override bool Equals(object x) => x == null ? false : Equals((LIB_Bitset)x);
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public override int GetHashCode()
        {
            var t = ary.Aggregate((a, x) => a ^ x);
            return (int)(((t >> 32) ^ t) & 0x00000000ffffffff);
        }
    }
    class LIB_String
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int[] ZAlgorithm(long[] s)
        {
            var ret = new int[s.Length];
            ret[0] = s.Length;
            int i = 1, j = 0;
            while (i < s.Length)
            {
                while (i + j < s.Length && s[j] == s[i + j]) ++j;
                ret[i] = j;
                if (j == 0) { ++i; continue; }
                var k = 1;
                while (i + k < s.Length && k + ret[k] < j) { ret[i + k] = ret[k]; ++k; }
                i += k; j -= k;
            }
            return ret;
        }
        /// <summary>
        /// 蜈･蜉帙・蜷・ｦ∫ｴ縺ｯ0莉･荳翫・1縺ｮ隕∫ｴ縺ｯ莉ｻ諢上→荳閾ｴ縲・        /// 繝槭ャ繝√＠縺滉ｽ咲ｽｮ縺ｮ1譁・ｭ礼岼縺ｮ菴咲ｽｮ繧・-indexed縺ｧ霑斐☆
        /// </summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static List<int> ShiftAnd(long[] s, long[] t)
        {
            var finish = new LIB_Bitset(t.Length);
            finish[t.Length - 1] = true;
            var mask = s.Where(e => e >= 0).Distinct().ToDictionary(e => e, _ => new LIB_Bitset(t.Length));
            var fil = new LIB_Bitset(t.Length);
            mask[-1] = ~fil;
            fil |= 1;
            foreach (var item in t)
            {
                if (item >= 0 && mask.ContainsKey(item)) mask[item] |= fil;
                if (item == -1) foreach (var item2 in mask.Keys.ToArray()) mask[item2] |= fil;
                fil <<= 1;
            }
            var state = new LIB_Bitset(s.Length);
            var ret = new List<int>();
            for (var i = 0; i < s.Length; i++)
            {
                state = ((state << 1) | 1) & mask[s[i]];
                if ((state & finish) == finish) ret.Add(i - t.Length + 1);
            }
            return ret;
        }
    }
    class LIB_BitUtil
    {
        public static readonly ulong[] BitMask;
        static LIB_BitUtil()
        {
            BitMask = new ulong[64];
            BitMask[0] = 1;
            for (var i = 1; i < 64; i++) BitMask[i] = BitMask[i - 1] << 1;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public IEnumerable<long> ScanOne(long value)
        {
            for (; value > 0; value &= value - 1) yield return value & -value;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public long CountOne(ulong value)
        {
            value = (value & 0x5555555555555555) + ((value >> 1) & 0x5555555555555555);
            value = (value & 0x3333333333333333) + ((value >> 2) & 0x3333333333333333);
            value = (value & 0x0f0f0f0f0f0f0f0f) + ((value >> 4) & 0x0f0f0f0f0f0f0f0f);
            value = (value & 0x00ff00ff00ff00ff) + ((value >> 8) & 0x00ff00ff00ff00ff);
            value = (value & 0x0000ffff0000ffff) + ((value >> 16) & 0x0000ffff0000ffff);
            value = (value & 0x00000000ffffffff) + ((value >> 32) & 0x00000000ffffffff);
            return (long)value;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public long CountOne(long value) => CountOne((ulong)value);
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public bool IsSet(ulong value, int idx) => (value & BitMask[idx]) != 0;
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public bool IsSet(long value, int idx) => IsSet((ulong)value, idx);
    }
}