Codeforces-191A-Dynasty Puzzles(简单dp)

A. Dynasty Puzzles
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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.

Examples
Input
3
abc
ca
cba
Output
6
Input
4
vvp
vvp
dam
vvp
Output
0
Input
3
ab
c
def
Output
1
Note

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

扫描二维码关注公众号,回复: 9348477 查看本文章


题意:给出n个字符串,若有两个字符串s1,s2,如果s1的尾和s2的首相同并且s1的首和s2的尾相同,则s1和s2可以连在一起,问最后能连在一起的最长字符串的长度;

思路:dp[i][j]表示以i开头以j结尾的字符串的长度,对于字符串str,头为s,尾为e,那么可以得到状态转移方程:dp[i][e]=max(dp[i][s]+strlen(str),dp[i][e]) 

AC代码;

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


发布了76 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_37171272/article/details/78195958
今日推荐