Problem:
Ambiguous Encoding

A friend of yours is designing an encoding scheme of a set of characters into a set of variable length bit sequences. You are asked to check whether the encoding is ambiguous or not. In an encoding scheme, characters are given distinct bit sequences of possibly different lengths as their codes. A character sequence is encoded into a bit sequence which is the concatenation of the codes of the characters in the string in the order of their appearances. An encoding scheme is said to be ambiguous if there exist two different character sequences encoded into exactly the same bit sequence. Such a bit sequence is called an “ambiguous binary sequence”.

For example, encoding characters “A”, “B”, and “C” to 0, 01 and 10, respectively, is ambiguous. This scheme encodes two different character strings “AC” and “BA” into the same bit sequence 010.

Input

The input consists of a single test case of the following format.

$n$
$w_1$
.
.
.
$w_n$

Here, $n$ is the size of the set of characters to encode ($1 \leq n \leq 1000$). The $i$-th line of the following $n$ lines, $w_i$, gives the bit sequence for the $i$-th character as a non-empty sequence of at most 16 binary digits, 0 or 1. Note that different characters are given different codes, that is, $w_i \ne w_j$ for $i \ne j$.

Output

If the given encoding is ambiguous, print in a line the number of bits in the shortest ambiguous binary sequence. Output zero, otherwise.

Sample Input 1

3
0
01
10

Sample Output 1

3

Sample Input 2

3
00
01
1

Sample Output 2

0

Sample Input 3

3
00
10
1

Sample Output 3

0

Sample Input 4

10
1001
1011
01000
00011
01011
1010
00100
10011
11110
0110

Sample Output 4

13

Sample Input 5

3
1101
1
10

Sample Output 5

4

Solution in C# (judge verdict: Accepted)

using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

public static class P
{
    static int[] lengths = new int[1 << (16 + 1)];
    static P()
    {
        for (int i = 0; i < lengths.Length; i++)
        {
            lengths[i] = Convert.ToString(i, 2).Length - 1;
        }
    }
    public static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        string[] s = Enumerable.Repeat(0, n).Select(_ => Console.ReadLine()).ToArray();
        int[] encoded = s.Select(x => Convert.ToInt32("1" + x, 2)).ToArray();
        int[] cost = new int[1 << (16 + 1)];
        bool[] exists = new bool[1 << (16 + 1)];
        bool[] wasFirst = new bool[1 << (16 + 1)];
        List<int>[] descendance = Enumerable.Repeat(0, 1 << (16 + 1)).Select(_ => new List<int>()).ToArray();
        PriorityQueue<Tuple<int, int>, int> pqueue = new PriorityQueue<Tuple<int, int>, int>(x => x.Item2, false);
        for (int i = 0; i < n; i++)
        {
            pqueue.Push(new Tuple<int, int>(encoded[i], s[i].Length));
            exists[encoded[i]] = true;
            for (int j = 1; (encoded[i] >> j) != 1; j++)
            {
                descendance[encoded[i] >> j].Add(encoded[i]);
            }
        }
        while (pqueue.Count > 0)
        {
            var elem = pqueue.Pop();
            var code = elem.Item1;
            var length = elem.Item2;
            if (!wasFirst[code] && cost[code] != 0) continue;
            cost[code] = length;
            wasFirst[code] = length == lengths[code];
            if (code == 1) break;
            //        54321
            //code : 1xxxxx
            //
            for (int i = length == lengths[code] ? 1 : 0; (code >> i) != 1; i++)
            {
                if (exists[code >> i])
                {
                    pqueue.Push(new Tuple<int, int>((1 << i) | (code & ((1 << i) - 1)), length));
                }
            }

            foreach (var item in descendance[code])
            {
                var overLength = (lengths[item] - lengths[code]);
                pqueue.Push(new Tuple<int, int>((1 << overLength) | (item & ((1 << overLength) - 1)), length + overLength));
            }
        }
        Console.WriteLine(cost[1]);
    }
}

class PriorityQueue<TValue, TKey> where TKey : IComparable<TKey>
{
    public int Count { get; private set; }
    private Func<TValue, TKey> KeySelector;
    private bool Descendance;
    private TValue[] data = new TValue[65536];
    private TKey[] keys = new TKey[65536];
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public PriorityQueue(Func<TValue, TKey> keySelector, bool descendance = false)
    { KeySelector = keySelector; Descendance = descendance; }
    public TValue Top
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get { ValidateNonEmpty(); return data[1]; }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public TValue Pop()
    {
        var top = Top;
        var item = data[Count--];
        var key = KeySelector(item);
        int index = 1;
        while (true)
        {
            if ((index << 1) >= Count)
            {
                if (index << 1 > Count) break;
                if (key.CompareTo(keys[index << 1]) > 0 ^ Descendance)
                { data[index] = data[index << 1]; keys[index] = keys[index << 1]; index <<= 1; }
                else break;
            }
            else
            {
                var nextIndex = keys[index << 1].CompareTo(keys[(index << 1) + 1]) <= 0 ^ Descendance ? (index << 1) : (index << 1) + 1;
                if (key.CompareTo(keys[nextIndex]) > 0 ^ Descendance)
                { data[index] = data[nextIndex]; keys[index] = keys[nextIndex]; index = nextIndex; }
                else break;
            }
        }
        data[index] = item;
        keys[index] = key;
        return top;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Push(TValue item)
    {
        var key = KeySelector(item);
        int index = ++Count;
        if (data.Length == Count) Extend(data.Length * 2);
        while ((index >> 1) != 0)
        {
            if (keys[index >> 1].CompareTo(key) > 0 ^ Descendance)
            { data[index] = data[index >> 1]; keys[index] = keys[index >> 1]; index >>= 1; }
            else break;
        }
        data[index] = item;
        keys[index] = key;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private void Extend(int newSize)
    {
        TValue[] newData = new TValue[newSize];
        TKey[] newKeys = new TKey[newSize];
        data.CopyTo(newData, 0);
        keys.CopyTo(newKeys, 0);
        data = newData;
    }
    private void ValidateNonEmpty() { if (Count == 0) throw new IndexOutOfRangeException(); }
}