CodeForces - 191A Dynasty Puzzles

來源:互聯網
上載者:User

標籤:des   style   blog   os   io   for   2014   art   

Description

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 ifA and B were kings, then kingA ruled before king B.

Input

The first line contains integer n (1?≤?n?≤?5·105) — the number of names in Vasya‘s list. Nextn lines contain n abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed10 characters.

Output

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 number0.

Sample Input

Input
3abccacba
Output
6
Input
4vvpvvpdamvvp
Output
0
Input
3abcdef
Output
1

題意:求能首尾組成環的最長的字串

思路:簡單的DP,枚舉26個字母,最後求dp[i][i]的最大值

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char a, b, str[20];int n, dp[30][30];int main() {scanf("%d", &n);memset(dp, 0, sizeof(dp));for (int i = 0; i < n; i++) {scanf("%s", str);int len = strlen(str);a = str[0] - 'a';b = str[len-1] - 'a';for (int i = 0; i < 26; i++) {if (dp[i][a] && dp[i][a]+len > dp[i][b])dp[i][b] = dp[i][a] + len;}if (dp[a][b] < len)dp[a][b] = len;}int ans = -1;for (int i = 0; i < 26; i++)ans = max(ans, dp[i][i]);printf("%d\n", ans);return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.