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)

#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 1005
#define MAX 16

struct Edge{
	Edge(int arg_word_id,int arg_loc,int arg_dist){
		word_id = arg_word_id;
		loc = arg_loc;
		dist = arg_dist;
	}
	int word_id,loc,dist;
};

struct Info{
	Info(int arg_word_id,int arg_loc,int arg_sum_dist){
		word_id = arg_word_id;
		loc = arg_loc;
		sum_dist = arg_sum_dist;
	}
	bool operator<(const struct Info &arg) const{

		return sum_dist > arg.sum_dist; //総距離の昇順(PQ)
	}
	int word_id,loc,sum_dist;
};
int N,AMBIGUOUS;
int min_dist[SIZE][MAX+1];
int LENGTH[SIZE];
char words[SIZE][MAX+1];
vector<Edge> G[SIZE][MAX+1];


int main(){

	scanf("%d",&N);
	AMBIGUOUS = N;

	int length;
	for(int i = 0; i < N; i++){

		scanf("%s",words[i]);
		for(length = 0; words[i][length] != '\0'; length++);
		LENGTH[i] = length;
	}

	int from_length,match_length;
	bool FLG;

	for(int from = 0; from < N; from++){
		for(int loc = 0; loc < LENGTH[from]; loc++){
			for(int to = 0; to < N; to++){
				if(from == to && loc == 0)continue;

				from_length = LENGTH[from]-loc;
				match_length = min(from_length,LENGTH[to]);

				FLG = true;
				for(int i = 0; i < match_length; i++){
					if(words[from][loc+i] != words[to][i]){
						FLG = false;
						break;
					}
				}
				if(!FLG)continue;

				if(match_length == LENGTH[to] && loc+match_length == LENGTH[from]){ //両方ぴったり終わる
					G[from][loc].push_back(Edge(AMBIGUOUS,0,LENGTH[to]));
				}else{

					if(from_length > match_length){
						G[from][loc].push_back(Edge(from,loc+match_length,match_length));

					}else{
						G[from][loc].push_back(Edge(to,match_length,match_length));
					}
				}
			}
		}
	}

	for(int i = 0; i <= N; i++){
		for(int k = 0; k < MAX; k++){
			min_dist[i][k] = BIG_NUM;
		}
	}

	priority_queue<Info> Q;

	for(int i = 0; i < N; i++){
		min_dist[i][0] = 0;
		Q.push(Info(i,0,0));
	}

	int next_word_id,next_loc,next_dist;

	while(!Q.empty()){

		if(Q.top().sum_dist > min_dist[Q.top().word_id][Q.top().loc]){

			Q.pop();
		}else{

			for(int i = 0; i < G[Q.top().word_id][Q.top().loc].size(); i++){

				next_word_id = G[Q.top().word_id][Q.top().loc][i].word_id;
				next_loc = G[Q.top().word_id][Q.top().loc][i].loc;
				next_dist = Q.top().sum_dist+G[Q.top().word_id][Q.top().loc][i].dist;

				if(min_dist[next_word_id][next_loc] > next_dist){
					min_dist[next_word_id][next_loc] = next_dist;
					Q.push(Info(next_word_id,next_loc,next_dist));
				}
			}
			Q.pop();
		}
	}

	if(min_dist[AMBIGUOUS][0] == BIG_NUM){

		printf("0\n");
	}else{

		printf("%d\n",min_dist[AMBIGUOUS][0]);
	}

	return 0;
}