Codeforces Round #121 (Div. 1) / 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 if A and B were kings, then king A ruled before king B.

Input

The first line contains integer n (1 ≤ n ≤ 5·105) — 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.

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 number 0.

Sample test(s)
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".


思路详见代码注释。


完整代码:

/*280ms,0KB*/

#include<cstring>
#include<cstdio>
const int N = 26;

int f[N][N];///记录以某字母开头和某字母结尾的朝代名长度
char s[11];

int main(void)
{
    int n;
	int head, tail, len;
	scanf("%d", &n);
	while (n--)
	{
		scanf("%s", s);
		len  = strlen(s);
		head = s[0] - 'a';
		tail = s[len - 1] - 'a';
		for (int i = 0; i < N; i++)
		///如果继承人满足“姓名条件”,且组成的朝代名比前面的继承人组成的还长,就更新
			if (f[i][head] && f[i][head] + len > f[i][tail])
				f[i][tail] = f[i][head] + len;
		if (len > f[head][tail])
            ///注意:他也可以直接当一个朝代的首位皇帝
			f[head][tail] =  len;
	}
	int ans = 0;
	for (int i = 0; i < N; i++)
		if (f[i][i] > ans)
			ans = f[i][i];///找到首尾字母相同中最大的
	printf("%d\n", ans);
	return 0;
}


优化:

/*92ms,0KB*/

#include<cstdio>

int map[40][40];
char arr[20];

int main()
{
	int i, j, k, l, max = 0;
	scanf("%d", &i);
	getchar();
	while (i--)
	{
		j = 0;
		while ((arr[j] = getchar()) != '\n')
            j++;
		arr[j] = 0;
		l = j;
		j--;
		for (k = 1; k < 28; k++)
			if (map[k][arr[0] & 31] && map[k][arr[0] & 31] + l > map[k][arr[j] & 31])
				map[k][arr[j] & 31] = map[k][arr[0] & 31] + l;
		if (map[arr[0] & 31][arr[j] & 31] < l)
			map[arr[0] & 31][arr[j] & 31] = l;
		if (max < map[arr[j] & 31][arr[j] & 31])
			max = map[arr[j] & 31][arr[j] & 31];
	}
	printf("%d\n", max);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值