You are given n strings s_1, s_2, ..., s_{n} consisting of characters 0 and 1. m operations are performed, on each of them you concatenate two existing strings into a new one. On the i-th operation the concatenation s_{a}_{i}s_{b}_{i} is saved into a new string s_{n} + i (the operations are numbered starting from 1). After each operation you need to find the maximum positive integer k such that all possible strings consisting of 0 and 1 of length k (there are 2^{k} such strings) are substrings of the new string. If there is no such k, print 0.


-----Input-----

The first line contains single integer n (1 ≤ n ≤ 100) — the number of strings. The next n lines contain strings s_1, s_2, ..., s_{n} (1 ≤ |s_{i}| ≤ 100), one per line. The total length of strings is not greater than 100.

The next line contains single integer m (1 ≤ m ≤ 100) — the number of operations. m lines follow, each of them contains two integers a_{i} abd b_{i} (1 ≤ a_{i}, b_{i} ≤ n + i - 1) — the number of strings that are concatenated to form s_{n} + i.


-----Output-----

Print m lines, each should contain one integer — the answer to the question after the corresponding operation.


-----Example-----
Input
5
01
10
101
11111
0
3
1 2
6 5
4 4

Output
1
2
0



-----Note-----

On the first operation, a new string "0110" is created. For k = 1 the two possible binary strings of length k are "0" and "1", they are substrings of the new string. For k = 2 and greater there exist strings of length k that do not appear in this string (for k = 2 such string is "00"). So the answer is 1.

On the second operation the string "01100" is created. Now all strings of length k = 2 are present.

On the third operation the string "1111111111" is created. There is no zero, so the answer is 0.

Difficulty: HARD
Tags: Bit manipulation

String algorithms

Implementation

Complete search

Dynamic programming
Skill types: Dynamic programming

Bit manipulation

Complete search

Starter code:

Solutions:
from sys import stdin, stdout
K = 20

def findAllStrings(s):
	n = len(s)
	sDict = {}
	for i in range(1, K + 1):
		sDict[i] = set()
		for x in range(n - i + 1):
			sDict[i].add(s[x:x + i])
	return sDict
n = int(stdin.readline().rstrip())
stringDicts = []
stringEnd = []
stringBegin = []
for i in range(n):
	s = stdin.readline().rstrip()
	stringDicts.append(findAllStrings(s))
	if len(s) < K:
		stringEnd.append(s)
		stringBegin.append(s)
	else:
		stringEnd.append(s[-20:])
		stringBegin.append(s[:20])
m = int(stdin.readline().rstrip())
for _ in range(m):
	(a, b) = map(int, stdin.readline().rstrip().split())
	a -= 1
	b -= 1
	sDict1 = findAllStrings(stringEnd[a] + stringBegin[b])
	sDict2 = stringDicts[a]
	sDict3 = stringDicts[b]
	sDict = {}
	for i in range(1, K + 1):
		sDict[i] = sDict1[i] | sDict2[i] | sDict3[i]
	stringDicts.append(sDict)
	for i in range(1, K + 1):
		if len(sDict[i]) != 2 ** i:
			print(i - 1)
			break
	if len(stringBegin[a]) < K and len(stringBegin[a]) + len(stringBegin[b]) < K:
		stringBegin.append(stringBegin[a] + stringBegin[b])
	elif len(stringBegin[a]) < K:
		s = stringBegin[a] + stringBegin[b]
		stringBegin.append(s[:K])
	else:
		stringBegin.append(stringBegin[a])
	if len(stringEnd[b]) < K and len(stringEnd[a]) + len(stringEnd[b]) < K:
		stringEnd.append(stringEnd[a] + stringEnd[b])
	elif len(stringEnd[b]) < K:
		s = stringEnd[a] + stringEnd[b]
		stringEnd.append(s[-K:])
	else:
		stringEnd.append(stringEnd[b])


from math import log
n = int(input())
p = [bin(p)[2:] for p in range(0, 512)]

def mset(s):
	ss = set()
	for k in range(0, 10):
		for pi in range(0, 2 ** k):
			cs = p[pi]
			cs = (k - len(cs)) * '0' + cs
			if cs in s:
				ss.add(cs)
	return ss

def q(s):
	for k in range(0, 10):
		for pi in range(0, 2 ** k):
			cs = p[pi]
			cs = (k - len(cs)) * '0' + cs
			if not cs in s:
				return k - 1
s = [[v[:9], v[-9:], mset(v)] for v in [input() for i in range(n)]]
for (qa, qb) in [[int(v) - 1 for v in input().split()] for i in range(int(input()))]:
	v = [s[qa][0], s[qb][1], mset(s[qa][1] + s[qb][0]) | s[qa][2] | s[qb][2]]
	if len(v[0]) < 9:
		v[0] = (v[0] + s[qb][0])[:9]
	if len(v[1]) < 9:
		v[1] = (s[qa][1] + s[qb][1])[-9:]
	s += [v]
	print(max(q(v[2]), 0))


from sys import stdin, stdout
k = 20

def findAllStrings(s):
	n = len(s)
	sdict = {}
	for i in range(1, k + 1):
		sdict[i] = set()
		for x in range(n - i + 1):
			sdict[i].add(s[x:x + i])
	return sdict
n = int(stdin.readline().rstrip())
strdict = []
stringend = []
stringbegin = []
for i in range(n):
	s = stdin.readline().rstrip()
	strdict.append(findAllStrings(s))
	if len(s) < k:
		stringend.append(s)
		stringbegin.append(s)
	else:
		stringend.append(s[-20:])
		stringbegin.append(s[:20])
m = int(stdin.readline().rstrip())
for _ in range(m):
	(a, b) = list(map(int, stdin.readline().rstrip().split()))
	a -= 1
	b -= 1
	sdict1 = findAllStrings(stringend[a] + stringbegin[b])
	sdict2 = strdict[a]
	sdict3 = strdict[b]
	sdict = {}
	for i in range(1, k + 1):
		sdict[i] = sdict1[i] | sdict2[i] | sdict3[i]
	strdict.append(sdict)
	for i in range(1, k + 1):
		if len(sdict[i]) != 2 ** i:
			print(i - 1)
			break
	if len(stringbegin[a]) < k and len(stringbegin[a]) + len(stringbegin[b]) < k:
		stringbegin.append(stringbegin[a] + stringbegin[b])
	elif len(stringbegin[a]) < k:
		s = stringbegin[a] + stringbegin[b]
		stringbegin.append(s[:k])
	else:
		stringbegin.append(stringbegin[a])
	if len(stringend[b]) < k and len(stringend[a]) + len(stringend[b]) < k:
		stringend.append(stringend[a] + stringend[b])
	elif len(stringend[b]) < k:
		s = stringend[a] + stringend[b]
		stringend.append(s[-k:])
	else:
		stringend.append(stringend[b])


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 15
mod = 10 ** 9 + 7

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def main():
	n = I()
	t = [[S(), [set() for _ in range(151)]] for _ in range(n)]
	m = I()
	q = [LI_() for _ in range(m)]
	for c in t:
		s = c[0]
		l = len(s)
		for i in range(1, min(151, l + 1)):
			for j in range(l - i + 1):
				c[1][i].add(s[j:j + i])
	rr = []
	for (li, ri) in q:
		l = t[li]
		r = t[ri]
		c = ['', [l[1][i] | r[1][i] for i in range(151)]]
		for i in range(1, min(150, len(l[0]) + 1)):
			ls = l[0][-i:]
			for j in range(1, min(151 - i, len(r[0]) + 1)):
				c[1][i + j].add(ls + r[0][:j])
		c[0] = l[0] + r[0]
		if len(c[0]) > 300:
			c[0] = c[0][:150] + c[0][-150:]
		t.append(c)
		r = 0
		for i in range(1, 151):
			tt = len(c[1][i])
			if tt != 2 ** i:
				break
			r = i
		rr.append(r)
	return '\n'.join(map(str, rr))
print(main())


import math
import collections
import bisect
import heapq
import time
import random
N = int(input())
S = ['']
for i in range(N):
	S.append(input())
M = int(input())
A = [[set() for _ in range(10)] for _ in range(M + N + 1)]
D = collections.defaultdict(int)
for i in range(1, N + 1):
	for j in range(1, 10):
		s = S[i]
		if j > len(s):
			break
		for k in range(len(s) - j + 1):
			A[i][j].add(int(s[k:k + j], 2))
		if all((v in A[i][j] for v in range(2 ** j))):
			D[i] = j
for i in range(M):
	(a, b) = list(map(int, input().split()))
	(s, sa, sb) = (S[a] + S[b], S[a], S[b])
	if len(s) > 30:
		S.append(s[:10] + s[-10:])
	else:
		S.append(s)
	ai = i + N + 1
	d = max(D[a], D[b]) + 1
	for dv in range(d, 10):
		if len(sa) + len(sb) < dv:
			break
		A[ai][dv] = A[a][dv] | A[b][dv] | {int(v, 2) for v in {sa[-i:] + sb[:dv - i] for i in range(1, dv + 1)} if len(v) == dv}
	ans = d - 1
	for dv in range(d, 10):
		if any((v not in A[ai][dv] for v in range(2 ** dv))):
			break
		ans = dv
	print(ans)
	D[ai] = ans