CodeForces-191A Dynasty Puzzles(动态规划DP)

A. DynastyPuzzles

The ancientBerlanders 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 somewhatinconvenient, 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, whosename was Victorious Vasily Pupkin, was always called by the berlanders VVP.

In Berland overits long history many dynasties of kings replaced each other, but they were allunited by common traditions. Thus, according to one Berland traditions, tomaintain stability in the country, the first name of the heir should be thesame as the last name his predecessor (hence, the first letter of theabbreviated name of the heir coincides with the last letter of the abbreviatedname of the predecessor). Berlanders appreciate stability, so this traditionhas never been broken. Also Berlanders like perfection, so another traditionrequires that the first name of the first king in the dynasty coincides withthe last name of the last king in this dynasty (hence, the first letter of theabbreviated name of the first king coincides with the last letter of the abbreviatedname of the last king). This tradition, of course, has also been alwaysobserved.

The name of adynasty is formed by very simple rules: we take all the short names of thekings in the order in which they ruled, and write them in one line. Thus, adynasty of kings "ab" and "ba" is called "abba",and the dynasty, which had only the king "abca", is called"abca".

Vasya, ahistorian, has recently found a list of abbreviated names of all Berland kingsand their relatives. Help Vasya to find the maximally long name of the dynastythat could have existed in Berland.

Note that in hislist all the names are ordered by the time, that is, if name A is earlierin the list than B, then if A and B werekings, then king A ruledbefore king B.

Input

The first linecontains integer n (1 ≤ n ≤ 5·105) — the numberof names in Vasya's list. Next n linescontain n abbreviated names, one per line. Anabbreviated name is a non-empty sequence of lowercase Latin letters. Its lengthdoes not exceed 10 characters.

Output

Print a singlenumber — length of the sought dynasty's name in letters.

If Vasya's listis wrong and no dynasty can be found there, print a single number 0.

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 firstsample two dynasties can exist: the one called "abcca" (with thefirst and second kings) and the one called "abccba" (with the firstand third kings).

In the secondsample there aren't acceptable dynasties.

The only dynastyin the third sample consists of one king, his name is "c".

 

 题目意思说:有一个王朝,他们国王的名字用姓氏的简写来标记每一代。

为了保证王朝的稳定,现在这个王朝的继承人的名字需要满足

继承者名字的第一个字母要和前代名字最后一个字母相同。

然后拼接起来的名字,第一个字母和最后一个字母相同

现在有一个考古博士,知道了这个王朝国王和亲戚的名字。问你这个王朝所能够得到的最长字符串。

 

解释一下样例

第一个最长字符串是abccba

第二个没有符合两个条件的人名

第三个以后c符合条件

 

数据范围比较大,用动态规划来写。

设DP方程,DP【i】【j】表示字符串的首字母是i,尾字母是j

所以有个DP转移方程,具体看代码比较好

dp【j】【v】=max(dp【j】【v】,dp【j】【u】+lena)【此时字符串a的首字母是u,尾字母是v】

当前表示之前的串(j,u)加上了当前串(u,v)的长度是否大于(j,v)串的长度,如果大于,更新状态

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#define inf 0x3f3f3f3f
using namespace std;
int n;
int dp[30][30];
char str[500005][20];
int main()
{
	int i,j;
	while(scanf("%d",&n)!=EOF)
	{
		memset(dp,0,sizeof dp);
		for(i=0;i<n;i++)
			scanf("%s",&str[i]);
		for(i=0;i<n;i++)
		{
			int len=strlen(str[i]);
			int u=str[i][0]-'a';
			int v=str[i][len-1]-'a';
			for(j=0;j<26;j++)
			{
				if(dp[j][u]==0)//前置字符为0,转到For循环外面的语句
					continue;//首尾相接,状态转移
				dp[j][v]=max(dp[j][v],dp[j][u]+len);//尾字符是V的字符,可以由尾字符U+这个字符串U开头V结尾
			}
			dp[u][v]=max(dp[u][v],len);
		}
		int maxn=-inf;
		for(i=0;i<26;i++)//枚举符合条件(最长字符串首尾字母相同)
			maxn=max(maxn,dp[i][i]);
		printf("%d\n",maxn);
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值