Codeforces 191A Dynasty Puzzles【dp】
来源:互联网 发布:51单片机视频教程下载 编辑:程序博客网 时间:2024/06/07 03:36
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 andB were kings, then kingA ruled before kingB.
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of names in Vasya's list. Nextn lines containn abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed10 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 number0.
3abccacba
6
4vvpvvpdamvvp
0
3abcdef
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".
题目大意:
一共给你N个字符串,如果一个字符串的尾部字母和一个字符串的首部字母相同,那么两个字符串就可以拼接起来,问最长的首尾字母相同的字符串的长度为多少。
思路:
1、我们设定dp【i】【j】表示首字母是i,尾字母是j的字符串的最长长度。
2、那么不难想到其状态转移方程:
dp【j】【v】=max(dp【j】【v】,dp【j】【u】+lena)【此时字符串a的首字母是u,尾字母是v】,当前表示之前的串(j,u)加上了当前串(u,v)的长度是否大于(j,v)串的长度,如果大于,更新dp【j】【v】。
dp【u】【v】=max(dp【u】【v】,lena);
那么也不难确定其状态转移的方向:
按照输入从1-N即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;char a[500400][14];int dp[27][27];int main(){ int n; while(~scanf("%d",&n)) { memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) { scanf("%s",a[i]); } for(int i=0;i<n;i++) { int u=a[i][0]-'a'; int lena=strlen(a[i]); int v=a[i][lena-1]-'a'; for(int j=0;j<26;j++) { if(dp[j][u]==0)continue; dp[j][v]=max(dp[j][v],dp[j][u]+lena); } dp[u][v]=max(dp[u][v],lena); } int maxn=0; for(int i=0;i<26;i++) { // printf("%d ",dp[i][i]); maxn=max(maxn,dp[i][i]); } //printf("\n"); printf("%d\n",maxn); }}
- codeforces 191A A. Dynasty Puzzles(dp)
- Codeforces 191A Dynasty Puzzles【dp】
- codeforces----191A Dynasty Puzzles
- codeforces 191A Dynasty Puzzles
- CodeForces - 191A Dynasty Puzzles
- CodeForces-191A Dynasty Puzzles(动态规划DP)
- Codeforces-191A-Dynasty Puzzles(简单dp)
- codeforces 191A Dynasty Puzzles(简单DP)
- CodeForces Round #121 (191A) - Dynasty Puzzles
- CF#191 A. Dynasty Puzzles (DP)
- Codeforces Round #121 (Div. 1) / 191A Dynasty Puzzles (dp&优化)
- CF 191A - Dynasty Puzzles(简单DP)
- CF A. Dynasty Puzzles
- Codeforces Round #121 (Div. 1) A. Dynasty Puzzles
- codeforces 191D Dynasty Puzzles (图论,疑难杂题)
- codeforces 192C Dynasty Puzzles 字符串动态规划
- CodeForces 337A Puzzles
- CodeForces 337A - Puzzles
- python连接mongodb数据库
- 常用正则表达式———为了少板砖
- Bison笔记
- python 获取系统相关编码的函数
- linux编程常用的命名
- Codeforces 191A Dynasty Puzzles【dp】
- VISA函数原型
- Linux系统核心源程序文件组织结构介绍
- HttpClient请求
- OpenGL中gl,glu,glut的区别
- spring学习之@ModelAttribute运用详解
- 磁盘访问时间计算
- (原创)使用aidl进程间通讯并开启独立进程的服务仿下载监听回调等场景
- Android7.0新特性介绍(二)——通知增强功能