“Shopee杯” 武汉大学(网络预选赛)A - A Monument For Heroes

题目链接:Click

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Months ago, the COVID-19 swept Wuhan. In order to control the outbreak, many people tried their best to help Wuhan, and with their help, we finally (almost) beat the virus, so we are going to set up a monument and write the names of all the contributors to express our gratitude to them. But there are so many people who have helped Wuhan so there are so many names to write, and how to arrange them is a big problem, so we decided that we will choose some representative names and rearrange their order to make them neatly. But until now we still do not know the maximum length of letters that will be write on the monument. We cannot leave enough space for these names without this number, so we leave this problem for you.

Now you will be given the name of all the contributors, and you can choose some and connect them in a certain order to make them neat. A permutation of names is neat only when for every component except the last one, the last letter of the component is the same as the first letter of the next component, and the last letter of the whole name is the same as the first one. E.g. if there are 3 contributors called ”abc”, “fga”, “cdef”, we can choose all these three names and combine them to “abccdeffga” to write on the monument with length of 10.

What’s more, these contributors agreed that one who contributed more is more important. They will provide you a preference list according to their agreement and you should connect the permutation strictly according to the list. That is, if a name s appears earlier than name t in the preference list, s would also appear earlier than tt if they are both selected into the permutations.

There will be many permutations which are neat, but we should leave enough space for every possible one, so can you tell us the length of the longest one?

Note: there might be multiple contributors have the same name.

输入描述

The first line contains integer n,(1 ≤ n ≤ 2*105) - the number of contributors. Next n lines contain n names, one per line. A contributor’s name is a non-empty sequence of lowercase Latin letters. Its length does not exceed 10 characters.
Meanwhile, this is a preference list, the name comes earlier should also appear earlier in your final permutation.

输出描述

Print a single number – the maximum length of combination of names. If there are no possible permutations, print 0 instead.

样例输入#1

3
abc
ca
cba

样例输出#1

6

说明#1

For the first example, the final perutation will be “abccba”.
Please note that “cbaabc” is against the rule that “abc” should be important than “cba”.

样例输入#2

4
aab
aab
lbwnb
mlslj

样例输出#2

0

题意

不看样例我还以为走错考场了呢,你确定这不是英语六级阅读理解?(还好我有Google翻译 )
大致意思是:

给你 n 个字符串,从中找一个最长子序列,要求该序列每一个字符串的最后一个字符和后一个字符串的第一个字符相等,并且,整个序列的第一个字符和最后一个字符相等

序列的长度为其中所有字符串的长度之和

分析

看n的大小,2e5,基本上死了暴力那条心(暴力大致要O(n2))
(虽然我还是作死来了一发,果然TLE )
那对于这种求子序列的题目,其实应该有点感觉,是dp,但dp的主要问题是,如何寻找状态转移方程,如何确定每个维度的含义。
如果直接让dp数组表示合法状态(即,符合题目要求的序列),那么将无法完成转移,因为合法状态可能要从非法状态转移而来(序列首尾字符不相等),所以我们将退让一步,让dp保留"最长"这个属性,而在dp结束后筛查"合法状态",以保证状态的顺利转移。

如此定义:dp[i][j] 代表首字符为 i ,尾字符为 j 的子序列的最大长度。
此时放宽了要求(首位字符不一定相等)

状态转移方程:
dp[i][j]=max( dp[i][j],dp[i][x]+len )
假设目前单词首字符为 x, 尾字符为 j,长度为 len

用rmap记录目前的首字符r,是否在尾部出现过,以保证能连接

用集合s记录以r字符结尾的子序列的首字符

OK,上代码
"Talk is Cheap. Show me the Code."

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<set>
using namespace std;
const int maxn = 2e5 + 5;
const int N = 128;
typedef long long ll;
char str[15];
bool rmap[N];
set<char>s[N];
int dp[N][N];
int main(void)
{int n;scanf("%d", &n);while (n--) {scanf("%s", str);int len = strlen(str);char l = str[0], r = str[len - 1];rmap[r] = true;s[r].insert(l);dp[l][r] = max(dp[l][r], len);//初值if (!rmap[l])continue;for (set<char>::iterator iter = s[l].begin(); iter != s[l].end(); iter++) {s[r].insert(*iter);//更新子序列首位字符对应dp[*iter][r] = max(dp[*iter][r], dp[*iter][l] + len);}}int ans = 0;for (int i = 0; i < N; i++)//筛查合法状态ans = max(ans, dp[i][i]);printf("%d\n", ans);return 0;
}

特别鸣谢:吴老板提供的技术支持

“Shopee杯” 武汉大学(网络预选赛)A - A Monument For Heroes相关推荐

  1. Greedy Sequence(2019南京icpc网络预选赛)主席树求区间小于k的最大值

    题意:给出n个整数,构造s1,s2,s3-sn s1,s2,s3-sns1,s2,s3-sn,si sisi满足五个条件 1.s1[i]=i s1[i]=is1[i]=i 2.对于1<j< ...

  2. 挑战程序设计竞赛_我系首次参加第六届中国大学生程序设计竞赛网络预选赛

    点击上方蓝字关注  「龙外信息工程系」 讲述有温度的故事    传递有态度的思想 2020年9月20日12时至17时,第六届中国大学生程序设计竞赛网络赛预选赛在杭州电子科技大学OJ成功举办,黑龙江外国 ...

  3. 武汉大学计算机专业网络安全,武汉大学网络空间安全考研科目有哪些?

    考研报考计算机专业,武汉大学无疑是最好的选择之一.那么,武汉大学计算机研究生考研科目是什么呢?武汉大学计算机学院下设通信与信息系统 (081001).模式识别与智能系统 (081104).计算机系统结 ...

  4. 2016广东工业大学新生杯决赛网络同步赛暨全国新生邀请赛

    2016广东工业大学新生杯决赛网络同步赛暨全国新生邀请赛 Ploblem A :   pigofzhou的巧克力棒 原题链接:http://gdutcode.sinaapp.com/problem.p ...

  5. “华为杯“ 武汉大学21级新生程序设计竞赛

    "华为杯" 武汉大学21级新生程序设计竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com) D.和谐之树(线段树) #i ...

  6. 第六届台达杯初赛网络考试——客观题总结

    第六届台达杯初赛网络考试--客观题总结 一.参赛总结 客观题总体还是比较简单的,也是比较容易拿分的,每个学校一般都是两个队参加初赛,也就是6个人,网络答题时可以先6个人共同答一份,然后逐次进行搜题.不 ...

  7. 本校扎堆报考,但高分并不是本校生!武汉大学网络空间安全学院

    武汉大学国家网络空间安全学院前段时间公布了今年考研的复试名单.与其他学校公布的复试名单不同,这所大学的复试名单居然还列出了考生的本科院校! 而更有意思的是,在复试名单中,竟然出现的大量的本校生! 究竟 ...

  8. “华为杯“武汉大学21级新生程序设计竞赛

    "华为杯"武汉大学21级新生程序设计竞赛 比赛传送门 u p d : 2022.4.8 upd:2022.4.8 upd:2022.4.8 补了 I I I题. B题待补. A - ...

  9. “图灵杯”趣味网络邀请赛 总结

    "图灵杯"趣味网络邀请赛 总结 总结 题出的还可以. A. 相遇 题目 比赛思路 看到 直接路程除以速度和. 骗了20分. B. 回文串 题目 比赛思路 又是构造,我最怕这种题. ...

最新文章

  1. 损失函数的可视化:浅论模型的参数空间与正则
  2. php mysql 主从复制_Windows 环境下,MySQL 的主从复制和主主复制
  3. mac下idea 13 在tomcat 7控制台乱码
  4. Django实现对数据库数据增删改查(一)
  5. 类操作是什么意思?jQuery的类操作教程分享
  6. Cloudera Manager 和CDH6.0.1安装,卸载,各步骤截图(此博文为笔者辛苦劳作最终生成的,使用了3个熬到凌晨2~4点的夜晚,外加一个周末完成,请转载时记录转载之处,谢谢)
  7. IOS开发基础之绘制饼图、柱状图、自定义进度条
  8. [学习总结]7、Android AsyncTask完全解析,带你从源码的角度彻底理解
  9. Failed to create the part‘s controls解决方法
  10. 在线文本纵向排版工具
  11. (二)洞悉linux下的Netfilteriptables:内核中的ip_tables小觑
  12. 【python】字符串转换整数 (atoi) - String
  13. 2021考研数学一123分经验分享
  14. office2010软件安装资料及教程
  15. Oracle Client卸载
  16. ROS入门(十)——两只小乌龟(乌龟跟随C++实现)
  17. 【AI人工智能学习】GitHub 上适合初学者的 10 个最佳开源 AI 项目
  18. opencv自定义从一幅图片截取特定区域函数cvExtractRegion
  19. Hbuilder 笔记
  20. 中国高科技、高成长50强

热门文章

  1. Terraform 学习总结(4)—— Terraform 实战
  2. 输入输出管理 用户管理
  3. 性能测试之nmon对linux服务器的监控
  4. loadrunner——常见函数
  5. ORACLE触发器(trigger)的使用
  6. 一文掌握 YUV 图像的基本处理
  7. IOS 使用BurpSuite抓包
  8. 解决java.io.IOException: Failed on local exception: com.google.protobuf.InvalidProtocolBufferException
  9. 网络通 永久免费 内网端口映射
  10. 布丁机器人可以一直插电吗_机器人布丁:不仅能播放,还能互动