csuoj1060: Nearest Sequence 动态规划
来源:互联网 发布:神圣罗马帝国历史 知乎 编辑:程序博客网 时间:2024/05/21 17:03
Description
Do you remember the "Nearest Numbers"? Now here comes its brother:"Nearest Sequence".Given three sequences of char,tell me the length of the longest common subsequence of the three sequences.
Input
There are several test cases.For each test case,the first line gives you the first sequence,the second line gives you the second one and the third line gives you the third one.(the max length of each sequence is 100)
Output
For each test case,print only one integer :the length of the longest common subsequence of the three sequences.
Sample Input
abcdabdcdbcaabcdcabdtsc
Sample Output
21
Description
Do you remember the "Nearest Numbers"? Now here comes its brother:"Nearest Sequence".Given three sequences of char,tell me the length of the longest common subsequence of the three sequences.
Input
There are several test cases.For each test case,the first line gives you the first sequence,the second line gives you the second one and the third line gives you the third one.(the max length of each sequence is 100)
Output
For each test case,print only one integer :the length of the longest common subsequence of the three sequences.
Sample Input
abcdabdcdbcaabcdcabdtsc
Sample Output
21
用dp开个三维数组,如果难以理解的话可以先做poj1458Common Subsequence这道题,它只要比较两个字符串 http://poj.org/problem?id=1458
我的这个代码还可以优化,不过A了就没管了,三个循环一个一个比过去就OK的。
AC代码:
#include<iostream>#include<cstring>using namespace std;char s1[105],s2[105],s3[105];int dp[105][105][105];int ans;void DP(){ memset(dp,0,sizeof dp); int n1=strlen(s1); int n2=strlen(s2); int n3=strlen(s3); for(int i=1;i<=n1;i++) for(int j=1;j<=n2;j++) for(int k=1;k<=n3;k++) { if(s1[i-1]==s2[j-1]&&s2[j-1]==s3[k-1]) dp[i][j][k]=dp[i-1][j-1][k-1]+1; else dp[i][j][k]=max(dp[i-1][j][k],max(dp[i][j-1][k],dp[i][j][k-1])); } ans=dp[n1][n2][n3];}int main(){ while(cin>>s1>>s2>>s3) { DP(); cout<<ans<<endl; } return 0;}
阅读全文
0 0
- csuoj1060: Nearest Sequence 动态规划
- POJ1141Brackets Sequence动态规划DP
- UVA10534 - Wavio Sequence - 动态规划
- 【动态规划】XMU 1583 Sequence
- HDU6078 Wavel Sequence(动态规划)
- csu 1060 Nearest Sequence
- Nearest Sequence(lcs)
- COJ-1060-Nearest Sequence
- POJ 1141 Brackets Sequence 动态规划
- 动态规划 Brackets Sequence poj 1141
- poj1141 Brackets Sequence 动态规划 输出路径
- UVA10534-----Wavio Sequence-----动态规划之LIS
- 动态规划 Brackets Sequence poj 1141
- NEU(1262: ASCII Sequence II)动态规划
- [动态规划]UVA348 - Optimal Array Multiplication Sequence
- POJ 1141 Brackets Sequence 动态规划
- poj 1458 Common Sequence--动态规划
- 【UVA】1626-Brackets sequence(动态规划)
- C语言从txt文件的数据保存到二维数组
- Django之models
- 对QT中connect函数的认识
- opencv 图像的膨胀和腐蚀(一)
- centos7.3+nginx1.8+php7.1+mysql5.7 安装(准备工作)
- csuoj1060: Nearest Sequence 动态规划
- fullPage.js
- 接前-后序遍历二叉树(非递归)
- 6月集训总结
- xshell远程时连接速度很慢的解决方法
- 关于css的优先级问题
- Java7并发编程指南——第八章:测试并发应用程序
- mysql 复制表
- Eclipse常用快捷键