Roman and Denis are on the trip to the programming competition. Since the trip was long, they soon got bored, and hence decided to came up with something. Roman invented a pizza's recipe, while Denis invented a string multiplication. According to Denis, the result of multiplication (product) of strings $s$ of length $m$ and $t$ is a string $t + s_1 + t + s_2 + \ldots + t + s_m + t$, where $s_i$ denotes the $i$-th symbol of the string $s$, and "+" denotes string concatenation. For example, the product of strings "abc" and "de" is a string "deadebdecde", while the product of the strings "ab" and "z" is a string "zazbz". Note, that unlike the numbers multiplication, the product of strings $s$ and $t$ is not necessarily equal to product of $t$ and $s$.

Roman was jealous of Denis, since he invented such a cool operation, and hence decided to invent something string-related too. Since Roman is beauty-lover, he decided to define the beauty of the string as the length of the longest substring, consisting of only one letter. For example, the beauty of the string "xayyaaabca" is equal to $3$, since there is a substring "aaa", while the beauty of the string "qwerqwer" is equal to $1$, since all neighboring symbols in it are different.

In order to entertain Roman, Denis wrote down $n$ strings $p_1, p_2, p_3, \ldots, p_n$ on the paper and asked him to calculate the beauty of the string $( \ldots (((p_1 \cdot p_2) \cdot p_3) \cdot \ldots ) \cdot p_n$, where $s \cdot t$ denotes a multiplication of strings $s$ and $t$. Roman hasn't fully realized how Denis's multiplication works, so he asked you for a help. Denis knows, that Roman is very impressionable, he guarantees, that the beauty of the resulting string is at most $10^9$.


-----Input-----

The first line contains a single integer $n$ ($2 \leq n \leq 100\,000$) — the number of strings, wroted by Denis.

Next $n$ lines contain non-empty strings $p_1, p_2, \ldots, p_n$, consisting of lowercase english letters.

It's guaranteed, that the total length of the strings $p_i$ is at most $100\,000$, and that's the beauty of the resulting product is at most $10^9$.


-----Output-----

Print exactly one integer — the beauty of the product of the strings.


-----Examples-----
Input
3
a
b
a

Output
3

Input
2
bnn
a

Output
1



-----Note-----

In the first example, the product of strings is equal to "abaaaba".

In the second example, the product of strings is equal to "abanana".

Source: codeforces | Difficulty: HARD
Tags: String algorithms

Dynamic programming

Greedy algorithms


Solutions:
t = [0] * 26

def get_k(c):
	return ord(c) - ord('a')

def analyze(s):
	length_of_str = len(s)
	pos = 0
	beauty_table = [0] * 26
	for i in range(1, length_of_str):
		if s[i] != s[pos]:
			k = get_k(s[pos])
			beauty_table[k] = max(beauty_table[k], i - pos)
			pos = i
	k = get_k(s[pos])
	beauty_table[k] = max(beauty_table[k], length_of_str - pos)
	pos = 0
	while pos < length_of_str and s[pos] == s[0]:
		pos += 1
	left_beauty = pos
	pos = length_of_str - 1
	while pos > 0 and s[pos] == s[length_of_str - 1]:
		pos -= 1
	right_beauty = length_of_str - pos - 1
	return (beauty_table, left_beauty, right_beauty)
r = []
for _ in range(int(input())):
	p = input()
	if all((x == p[0] for x in p)):
		k = get_k(p[0])
		for i in range(26):
			if i == k:
				t[i] = len(p) * (t[i] + 1) + t[i]
			else:
				t[i] = min(1, t[i])
	else:
		for i in range(26):
			t[i] = min(1, t[i])
		(bt, lb, rb) = analyze(p)
		(lk, rk) = (get_k(p[0]), get_k(p[-1]))
		if lk == rk:
			t[lk] = lb + rb + t[lk]
		else:
			(t[lk], t[rk]) = (t[lk] + lb, t[rk] + rb)
		for i in range(26):
			t[i] = max(t[i], bt[i])
print(max(t))


from math import *
import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return list(map(int, minp().split()))
n = mint()
a = [0] * 256
b = [0] * 256
for k in range(0, n):
	for i in range(ord('a'), ord('z') + 1):
		b[i] = min(a[i], 1)
	i = 0
	s = list(minp())
	l = len(s)
	q = 0
	while i < l:
		j = i + 1
		while j < l and s[j] == s[i]:
			j += 1
		z = ord(s[i])
		w = j - i
		if i == 0:
			q = w
			if j == l:
				w += (w + 1) * a[z]
			elif a[z] != 0:
				w += 1
		elif j == l:
			w += 1
			if s[0] == s[-1]:
				w += q
		b[z] = max(b[z], w)
		i = j
	(a, b) = (b, a)
print(max(a))


import sys
from collections import Counter
readline = sys.stdin.readline
N = int(readline())
S = [readline().strip() for i in range(N)]

def calc(s):
	res = []
	last = s[0]
	cnt = 0
	for c in s:
		if c == last:
			cnt += 1
		else:
			res.append((last, cnt))
			last = c
			cnt = 1
	if cnt > 0:
		res.append((last, cnt))
	return res
rr = calc(S[N - 1])
if len(rr) > 1:
	ans = max((x for (c, x) in rr))
	(c0, x0) = rr[0]
	(c1, x1) = rr[-1]
	m0 = m1 = 0
	for i in range(N - 1):
		s = S[i]
		for c in s:
			if c1 == c == c0:
				ans = max(ans, x0 + x1 + 1)
			elif c == c0:
				ans = max(ans, x0 + 1)
			elif c == c1:
				ans = max(ans, x1 + 1)
else:
	(c0, x0) = rr[0]
	rr0 = calc(S[0])
	r = 0
	for (c, x) in rr0:
		if c0 == c:
			r = max(r, x)
	for i in range(1, N - 1):
		rr1 = calc(S[i])
		if len(rr1) == 1:
			(c1, x1) = rr1[0]
			if c0 == c1:
				r = (r + 1) * x1 + r
			else:
				r = +(r > 0)
		else:
			(d0, y0) = rr1[0]
			(d1, y1) = rr1[-1]
			if c0 != d0:
				y0 = 0
			if c0 != d1:
				y1 = 0
			if r > 0:
				r = y0 + y1 + 1
			else:
				r = max(y0, y1)
			for (d, y) in rr1:
				if d == c0:
					r = max(r, y)
	ans = (r + 1) * x0 + r
print(ans)


import os
from io import BytesIO
from collections import namedtuple
Parsed = namedtuple('Parsed', 'type p pl s sl')
(D, U) = (0, 1)

def parse(s):
	(pc, sc) = (0, 0)
	for c in s:
		if c != s[0]:
			break
		pc += 1
	for c in reversed(s):
		if c != s[-1]:
			break
		sc += 1
	if s[0] == s[-1] and pc == sc == len(s):
		tp = U
	else:
		tp = D
	return Parsed(tp, s[0], pc, s[-1], sc)

def max_conti_len(s, target):
	mx = 0
	cur = 0
	for c in s:
		if c == target:
			cur += 1
			mx = max(mx, cur)
		else:
			cur = 0
	return mx

def len_mul(nl, ol):
	return ol * nl + ol + nl

def solve(n, ss):
	s = ss.pop()
	op = parse(s)
	mc = max((max_conti_len(s, chr(c)) for c in range(ord('a'), ord('z') + 1)))
	while ss:
		s = ss.pop()
		np = parse(s)
		if np.type == U and op.type == U:
			if np.p == op.p:
				nl = len_mul(np.pl, op.pl)
				op = Parsed(U, op.p, nl, op.s, nl)
			else:
				op = Parsed(D, op.p, op.pl, op.s, op.sl)
			mc = max(mc, op.pl)
		elif np.type == D and op.type == U:
			npl = len_mul(np.pl, op.pl) if np.p == op.p else op.pl
			nsl = len_mul(np.sl, op.sl) if np.s == op.s else op.sl
			mx = max_conti_len(s, op.s)
			mc = max(mc, len_mul(mx, op.pl))
			op = Parsed(D, op.p, npl, op.s, nsl)
		elif op.type == D:
			if op.p == op.s:
				mp = op.pl + op.sl + 1 if op.p in s else op.pl
				ms = op.sl
			else:
				mp = op.pl + 1 if op.p in s else op.pl
				ms = op.sl + 1 if op.s in s else op.sl
			mc = max(mc, mp, ms)
	print(mc)

def solve_from_stdin():
	n = int(input())
	ss = []
	for _ in range(n):
		ss.append(input())
	solve(n, ss)
solve_from_stdin()


def prog(mass, st):
	nonlocal alf
	if st == st[0] * len(st):
		for i in range(26):
			if alf[i] == st[0]:
				mass[i] = (mass[i] + 1) * len(st) + mass[i]
			else:
				mass[i] = min(1, mass[i])
	else:
		mmm = razlog(st)
		r = 1
		while st[r] == st[r - 1]:
			r += 1
		k = 1
		while st[len(st) - k] == st[len(st) - k - 1]:
			k += 1
		for i in range(26):
			if alf[i] == st[0] and alf[i] == st[-1]:
				if mass[i] == 0:
					mass[i] = max(mmm[i], k, r)
				else:
					mass[i] = max(mmm[i], k + r + 1)
			elif alf[i] == st[0]:
				if mass[i] == 0:
					mass[i] = max(mmm[i], r)
				else:
					mass[i] = max(mmm[i], r + 1)
			elif alf[i] == st[-1]:
				if mass[i] == 0:
					mass[i] = max(mmm[i], k)
				else:
					mass[i] = max(mmm[i], k + 1)
			elif mass[i] == 0:
				mass[i] = mmm[i]
			else:
				mass[i] = max(1, mmm[i])
	return mass

def razlog(st):
	nonlocal alf
	mass = [0 for i in range(26)]
	mass[alf.index(st[0])] = 1
	now = 1
	for i in range(1, len(st)):
		if st[i] == st[i - 1]:
			now += 1
		else:
			now = 1
		mass[alf.index(st[i])] = max(now, mass[alf.index(st[i])])
	return mass
n = int(input())
st = input()
alf = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
mass = razlog(st)
for i in range(n - 1):
	sti = input()
	mass = prog(mass, sti)
print(max(mass))


n = int(input())

def get_longest(s):
	prev_c = -1
	curr_len = 0
	longest = [0] * 26
	for c in s:
		if c != prev_c:
			curr_len = 1
			prev_c = c
		else:
			curr_len += 1
		longest[c] = max(longest[c], curr_len)
	return longest

def get_prefix(s):
	prev_c = s[0]
	curr_len = 0
	for c in s:
		if c == prev_c:
			curr_len += 1
		else:
			return (prev_c, curr_len)
	return (prev_c, curr_len)

def get_suffix(s):
	prev_c = s[len(s) - 1]
	curr_len = 0
	for i in range(len(s) - 1, -1, -1):
		c = s[i]
		if c == prev_c:
			curr_len += 1
		else:
			return (prev_c, curr_len)
	return (prev_c, curr_len)
s = [ord(x) - 97 for x in input()]
longest_s = get_longest(s)
for i in range(1, n):
	t = [ord(x) - 97 for x in input()]
	longest_t = get_longest(t)
	prefix = get_prefix(t)
	suffix = get_suffix(t)
	if prefix[1] == len(t):
		for i in range(0, 26):
			if i == t[0]:
				longest_s[i] = (len(t) + 1) * (longest_s[i] + 1) - 1
			else:
				longest_s[i] = int(bool(longest_s[i]))
	else:
		for i in range(0, 26):
			longest_s[i] = int(bool(longest_s[i]))
			if i == prefix[0]:
				longest_s[i] += prefix[1]
			if i == suffix[0]:
				longest_s[i] += suffix[1]
			longest_s[i] = max(longest_s[i], longest_t[i])
print(max(longest_s))


def mult(s, t):
	(f1, l1, cntf1, cntl1, beaut1, n1) = s
	(f2, l2, cntf2, cntl2, beaut2, n2, p) = t
	(f3, l3, cntf3, cntl3, beaut3, n3) = (0, 0, 0, 0, beaut1, 0)
	f3 = f1
	l3 = l1
	n3 = n1 * (n2 + 1) + n2
	if cntf1 >= n1 and f1 == f2:
		cntf3 = n1 * (cntf2 + 1) + cntf2
	else:
		cntf3 = cntf1
	if cntl1 == n1 and l1 == l2:
		cntl3 = n1 * (cntl2 + 1) + cntl2
	else:
		cntl3 = cntl1
	if f1 != l1:
		if f1 in p:
			beaut3 = max(beaut3, cntf1 + 1)
		if l1 in p:
			beaut3 = max(beaut3, cntl1 + 1)
	elif cntf1 >= n1:
		beaut3 = max(n1, beaut3)
		ans = 0
		h = 0
		for d in p:
			if d == f1:
				h += 1
				ans = max(ans, h)
			else:
				h = 0
		ans = max(ans, h)
		beaut3 = max(beaut3, n1 * (ans + 1) + ans)
	elif f1 in p:
		beaut3 = max(beaut3, 1 + cntf1 + cntl1)
	else:
		beaut3 = max(beaut3, cntf1, cntl1)
	return [f3, l3, cntf3, cntl3, beaut3, n3]
n = int(input())
p = []
for i in range(n):
	p.append(input())
pp = []
for s in p:
	f = s[0]
	l = s[-1]
	cntf = 0
	cntl = 0
	beaut = 1
	hep = 1
	for i in s:
		if i == f:
			cntf += 1
		else:
			break
	for i in s[::-1]:
		if i == l:
			cntl += 1
		else:
			break
	for i in range(1, len(s)):
		if s[i] == s[i - 1]:
			hep += 1
		else:
			beaut = max(beaut, hep)
			hep = 1
	beaut = max(beaut, hep)
	pp.append([f, l, cntf, cntl, beaut, len(s), s])
p = pp[::-1]
lasts = p[0][:-1]
for i in range(1, len(p)):
	lasts = mult(lasts, p[i])
print(lasts[-2])


ALPH = 'abcdefghijklmnopqrstuvwxyz'
MAX = 10 ** 9

def cnt(s):
	c = {ch: 0 for ch in ALPH}
	i = 0
	while i < len(s):
		j = i + 1
		while j < len(s) and s[i] == s[j]:
			j += 1
		c[s[i]] = max(c[s[i]], j - i)
		i = j
	return c

def nxt(c, t):
	nc = cnt(t)
	for ch in ALPH:
		if c[ch] and (not nc[ch]):
			nc[ch] = 1
	f = 0
	while f < len(t) and t[f] == t[0]:
		f += 1
	r = 0
	while r < len(t) and t[-1 - r] == t[-1]:
		r += 1
	if t[0] == t[-1]:
		if f == len(t):
			nc[t[0]] = max(nc[t[0]], c[t[0]] + (c[t[0]] + 1) * len(t))
		elif c[t[0]]:
			nc[t[0]] = max(nc[t[0]], f + 1 + r)
	else:
		nc[t[0]] = max(nc[t[0]], f + (c[t[0]] > 0))
		nc[t[-1]] = max(nc[t[-1]], r + (c[t[-1]] > 0))
	return {x: min(MAX, y) for (x, y) in nc.items()}
n = int(input())
c = cnt(input())
for i in range(n - 1):
	c = nxt(c, input())
print(max(c.values()))


n = input()
n = int(n)
p = []
substring_maxlen = 0
str_info = []
_temper = ['abba', 'a']
for i in range(n):
	string = input()
	str_info.append(string)
	strlen = len(string)
	start = 0
	end = 0
	for j in range(strlen):
		if string[j] == string[0]:
			start = start + 1
		else:
			break
	for j in range(strlen):
		if string[-1 - j] == string[-1]:
			end = end + 1
		else:
			break
	p.append((string[0], start, string[-1], end, True if strlen == start else False))
_max_len = 0
parse = 0
_temp_max = 0
string = str_info[-1]
token = string[0]
while parse < len(string):
	token = string[parse]
	_temp_max = 0
	for k in range(parse, len(string)):
		if string[k] == token:
			parse = parse + 1
			_temp_max = _temp_max + 1
		else:
			break
	if substring_maxlen < _temp_max:
		substring_maxlen = _temp_max
start_token = []
end_token = []
(start_token, start_num, end_token, end_num, connected) = p[-1]
level = 0
for i in range(1, len(p)):
	if not connected:
		break
	else:
		_string = str_info[-i - 1]
		_max_len = 0
		parse = 0
		_temp_max = 0
		token = _string[0]
		_substring_maxlen = 0
		while parse < len(_string):
			token = _string[parse]
			if token != start_token:
				parse = parse + 1
				continue
			_temp_max = 0
			for k in range(parse, len(_string)):
				if _string[k] == token:
					parse = parse + 1
					_temp_max = _temp_max + 1
				else:
					break
			if _substring_maxlen < _temp_max:
				_substring_maxlen = _temp_max
		substring_maxlen = max(substring_maxlen, start_num * (_substring_maxlen + 1) + _substring_maxlen)
		(_start_token, _start_num, _end_token, _end_num, _connected) = p[-1 - i]
		if _start_token == start_token:
			start_num = start_num * (_start_num + 1) + _start_num
		if _end_token == end_token:
			end_num = end_num * (_end_num + 1) + _end_num
		if not _connected or _start_token != start_token:
			connected = False
		level = i
end_cond = 0
if start_num > end_num:
	end_cond = 1
elif start_num < end_num:
	end_cond = 2
the_End = False
answer = max(start_num, end_num) + 1
for i in range(len(p) - level - 1):
	for s in str_info[i]:
		if start_token == s:
			if end_cond < 2:
				the_End = True
				if start_token == end_token:
					answer = answer + min(start_num, end_num)
				break
		if end_token == s:
			if end_cond % 2 == 0:
				the_End = True
				if start_token == end_token:
					answer = answer + min(start_num, end_num)
				break
	if the_End:
		break
else:
	answer = answer - 1
if len(p) == 1:
	answer = answer - 1
print(max(answer, substring_maxlen))