poj 1458 Common Subsequence
来源:互联网 发布:北京百知尚行是培训吗 编辑:程序博客网 时间:2024/05/28 15:53
Common Subsequence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 47381 Accepted: 19464
Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, …, xm > another sequence Z = < z1, z2, …, zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, …, ik > of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.
Output
For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
【分析】
突然发现刷水题成为了我的乐趣。。
这道题就是求最长公共子序列
dp[i][j]表示a串的前i个字符和b串的前j个字符所能组成的最长公共子序列长度
然后推出状态转移方程(具体见代码)
【代码】
//poj 1458 Common Subsequence#include<iostream>#include<cstdio>#include<cstring>#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;string a,b;int dp[1001][1001];int main(){ int i,j,k; while(cin>>a>>b) { memset(dp,0,sizeof dp); int la=a.size()-1; int lb=b.size()-1; fo(i,0,la) fo(j,0,lb) { if(a[i]==b[j]) dp[i+1][j+1]=dp[i][j]+1; else dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]); } printf("%d\n",dp[la+1][lb+1]); } return 0;}
【附上另一份脑残代码】
【唯一不同就是下标的不同,这里的dp[i][j]表示i之前与j之前的串】
//poj 1458 Common Subsequence#include<iostream>#include<cstdio>#include<cstring>#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;string a,b;int dp[1001][1001];int main(){ int i,j,k; while(cin>>a>>b) { memset(dp,0,sizeof dp); int la=a.size()-1; int lb=b.size()-1; fo(i,0,la) fo(j,0,lb) { if(a[i]==b[j]) if(i-1>=0 && j-1>=0) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=1; else { if(i-1>=0) dp[i][j]=max(dp[i-1][j],dp[i][j]); if(j-1>=0) dp[i][j]=max(dp[i][j-1],dp[i][j]); } } printf("%d\n",dp[la][lb]); } return 0;}
- Common Subsequence--poj--1458
- poj 1458 Common Subsequence
- poj 1458 Common Subsequence
- Poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ-1458-Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- POJ 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- KMP算法
- POJ 2479 DP
- 数值的整数次方
- 字符串匹配的KMP算法和Boyer-Moore算法
- hdu 1010Tempter of the Bone(dfs+剪枝)
- poj 1458 Common Subsequence
- Spring Boot / Spring MVC 入门实践 (二) :MVC与Spring MVC简介
- java继承
- list_head用法(二) 接口函数list_for_each_entry
- Android 开发工程师面试指南
- C重要知识点&疑惑点
- Algorithms for Detecting Significantly Mutated Pathways in Cancer
- python核心编程作业--正则表达式和图形界面--电影搜索器(电影天堂)
- Android消息处理机制:Handler|Message