301011: CF191A. Dynasty Puzzles

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Dynasty Puzzles

题意翻译

### 题目描述 有一个王朝,他们国王的名字用姓氏的简写来标记每一代。为了保证王朝的稳定,现在这个王朝的继承人的名字需要满足继承者名字的第一个字母要和前代名字最后一个字母相同。然后拼接起来的名字,第一个字母和最后一个字母相同。现在有一个考古博士,知道了这个王朝国王和亲戚的名字。问你这个王朝所能够得到的最长字符串。 ### 输入 第一行一个整数n(1≤n≤5·1e5),接下来n行,每行一个非空字符串,全由小写字母组成,字符串长度不超过10 ### 输出 最长满足要求的长度,如果没有输出0

题目描述

The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the king, whose name was Victorious Vasily Pupkin, was always called by the berlanders VVP. In Berland over its long history many dynasties of kings replaced each other, but they were all united by common traditions. Thus, according to one Berland traditions, to maintain stability in the country, the first name of the heir should be the same as the last name his predecessor (hence, the first letter of the abbreviated name of the heir coincides with the last letter of the abbreviated name of the predecessor). Berlanders appreciate stability, so this tradition has never been broken. Also Berlanders like perfection, so another tradition requires that the first name of the first king in the dynasty coincides with the last name of the last king in this dynasty (hence, the first letter of the abbreviated name of the first king coincides with the last letter of the abbreviated name of the last king). This tradition, of course, has also been always observed. The name of a dynasty is formed by very simple rules: we take all the short names of the kings in the order in which they ruled, and write them in one line. Thus, a dynasty of kings "ab" and "ba" is called "abba", and the dynasty, which had only the king "abca", is called "abca". Vasya, a historian, has recently found a list of abbreviated names of all Berland kings and their relatives. Help Vasya to find the maximally long name of the dynasty that could have existed in Berland. Note that in his list all the names are ordered by the time, that is, if name $ A $ is earlier in the list than $ B $ , then if $ A $ and $ B $ were kings, then king $ A $ ruled before king $ B $ .

输入输出格式

输入格式


The first line contains integer $ n $ ( $ 1<=n<=5·10^{5} $ ) — the number of names in Vasya's list. Next $ n $ lines contain $ n $ abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed $ 10 $ characters.

输出格式


Print a single number — length of the sought dynasty's name in letters. If Vasya's list is wrong and no dynasty can be found there, print a single number $ 0 $ .

输入输出样例

输入样例 #1

3
abc
ca
cba

输出样例 #1

6

输入样例 #2

4
vvp
vvp
dam
vvp

输出样例 #2

0

输入样例 #3

3
ab
c
def

输出样例 #3

1

说明

In the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings). In the second sample there aren't acceptable dynasties. The only dynasty in the third sample consists of one king, his name is "c".

Input

题意翻译

### 题目描述 有一个王朝,他们国王的名字用姓氏的简写来标记每一代。为了保证王朝的稳定,现在这个王朝的继承人的名字需要满足继承者名字的第一个字母要和前代名字最后一个字母相同。然后拼接起来的名字,第一个字母和最后一个字母相同。现在有一个考古博士,知道了这个王朝国王和亲戚的名字。问你这个王朝所能够得到的最长字符串。 ### 输入 第一行一个整数n(1≤n≤5·1e5),接下来n行,每行一个非空字符串,全由小写字母组成,字符串长度不超过10 ### 输出 最长满足要求的长度,如果没有输出0

加入题单

上一题 下一题 算法标签: