CodeForces 191A Dynasty Puzzles(DP)

系统管理员 2022-06-02 04:00 178阅读 0赞

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 10characters.

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.

Example

Input

  1. 3
  2. abc
  3. ca
  4. cba

Output

  1. 6

Input

  1. 4
  2. vvp
  3. vvp
  4. dam
  5. vvp

Output

  1. 0

Input

  1. 3
  2. ab
  3. c
  4. def

Output

  1. 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”.

题解:

这题一看是dp就差点放弃了,然后随便搜了看到学姐的博客发现这题其实不难:

题意是给你一堆串,只要一个串的尾字符和另一个串的头字符相同就可以合并,问你最终可以得到头尾字符相同的串的最长长度为多少

数组a[i][j]表示字符串编号为i+‘a’,结尾为j+’a’的串的最长长度,就可以开始dp了

对于一个串串长为len,记串开头字符下标为st,结尾字符下标为ed

遍历开头为从a到z的数组a[p][st],也就是遍历所有p<26,做a[p][ed]=max(a[p][ed],a[p][st]+len)

最后从i=0到25遍历一遍a[i][i]取最大值就是答案

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<queue>
  5. #include<stack>
  6. #include<math.h>
  7. #include<vector>
  8. #include<map>
  9. #include<set>
  10. #include<stdlib.h>
  11. #include<cmath>
  12. #include<string>
  13. #include<algorithm>
  14. #include<iostream>
  15. #include<stdio.h>
  16. using namespace std;
  17. #define ll long long
  18. int a[30][30];
  19. char s[15];
  20. int main()
  21. {
  22. int i,j,n;
  23. scanf("%d",&n);
  24. memset(a,0,sizeof(a));
  25. for(i=0;i<n;i++)
  26. {
  27. scanf("%s",s);
  28. int len=strlen(s);
  29. int st=s[0]-'a';
  30. int ed=s[len-1]-'a';
  31. for(j=0;j<26;j++)
  32. {
  33. if(a[j][st])
  34. {
  35. a[j][ed]=max(a[j][ed],a[j][st]+len);
  36. }
  37. }
  38. a[st][ed]=max(a[st][ed],len);
  39. }
  40. int maxx=0;
  41. for(i=0;i<26;i++)
  42. {
  43. if(a[i][i]>maxx)
  44. {
  45. maxx=a[i][i];
  46. }
  47. }
  48. printf("%d\n",maxx);
  49. return 0;
  50. }

发表评论

表情:
评论列表 (有 0 条评论,178人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CodeForces679A

    [CodeForces679A][] 也是交互题,这个要稍微难一些. 考虑的过程大概是: \\(1\\)肯定没有问的价值,如果问过\\(2\\),那么除了\\(4\

    相关 CodeForces1214A

    [CodeForces1214A][] 说起来你们可能不信,这题硬生生卡了我\\(1h\\),我想了背包,扩欧,二分....等等一坨办法.结果最后还是用了\\(bfs\\)

    相关 codeforce 141A

    /字符串问题 没AC的人可能是没看清楚题目吧, 先大概说下题目大意: 给你3个字符串,如果第一个串和第二个串组合在一起可以等于第三个串就输出“Y