CodeForces
来源:互联网 发布:天刀男捏脸数据公子羽 编辑:程序博客网 时间:2024/06/06 07:34
题目描述:点击打开链接
题目比较长但是题意却没有很复杂,现给你n个字符串,如果一个字符串的尾与另一个字符串的首相同,那么这两个字符串就可以相连,要求的是最长的首尾相同的字符串长度。
定义dp[i][j]表示以i开头以j结尾的字符串的最长长度,那么对于n个字符串我们只考虑将它连到前面的字符串上,所以状态转移就简单的多了,枚举已经连接的最长长度去更新就好了。
AC代码:
#include<iostream>#include<sstream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<stack>#include<queue>#include<algorithm>using namespace std;const int MOD=1e9+7;const int MAXM=1e5;int dp[31][31];string s[5*MAXM+10];int main(){ int n; scanf("%d",&n); for (int i=0;i<n;i++) cin>>s[i]; memset(dp,0,sizeof(dp)); for (int i=0;i<n;i++) { int len=s[i].size(); int c1=s[i][0]-'a'; int c2=s[i][len-1]-'a'; for (int j=0;j<26;j++) { if (dp[j][c1]!=0)//表示存在这种形式的字符串,保证了状态转移的正确性。 dp[j][c2]=max(dp[j][c2],dp[j][c1]+len); } dp[c1][c2]=max(dp[c1][c2],len); } int ans=0; // for (int i=0;i<26;i++) //printf("%c--%d\n",'a'+i,dp[i][i]); for (int i=0;i<26;i++) ans=max(ans,dp[i][i]); printf("%d\n",ans); return 0;}
阅读全文
0 0
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Redis源码分析(三)——Redis数据结构-字典
- linux nginx完全卸载
- QmlImageProvider
- Kafka架构图
- iOS与网页JS交互,看我就够了
- CodeForces
- A. Bus to Udayland
- tensorflow手册实现mnist数据集的CNN
- tf install
- 问题:The resource identified by this request is only capable of generating responses with characterist
- 测试分布式系统的线性一致性
- 使用Spring注解AOP(基于自定义注解和包下拦截方法)
- HDU-5974-A Simple Math Problem
- 解决keil打开工程无响应