POJ 1458 Common Subsequence
来源:互联网 发布:hdu2009 java 编辑:程序博客网 时间:2024/06/11 22:05
Common Subsequence
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 30985 Accepted: 12073
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 abfcabprogramming contest abcd mnp
Sample Output
420
Source
Southeastern Europe 2003
解题思路:题目说那么多都不用管,就是求两串的最大公共子序列,因为在求的时候,每个状态都已经保证了在第二个序列中的子序列序号是递增的。
dp[i][j]表示第一个串的前i个字母和第二个串的前j个字母配对的最大匹配数,初始化为dp[i][j]={0},
状态转移方程为dp[i][j]={dp[i-1][j-1]+1(若s1[i-1]==s2[j-1]),max(dp[i-1][j],dp[i][j-1])};
#include<iostream>using namespace std;inline int max(int a,int b){return a>b?a:b;}int main(){char s1[500],s2[500];int i,j,len1,len2,dp[500][500];while(cin>>s1>>s2){memset(dp,0,sizeof(dp)); len1=strlen(s1);len2=strlen(s2); for(i=1;i<=len1;i++)for(j=1;j<=len2;j++){if(s1[i-1]==s2[j-1])dp[i][j]=dp[i-1][j-1]+1;elsedp[i][j]=max(dp[i-1][j],dp[i][j-1]);}cout<<dp[len1][len2]<<endl;}}
- 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
- 正则表达式30分钟入门教程
- 常用系统宏定义
- EasyUI datagrid editor checkbox
- live555 源码解析 --H264VideoStreamParser 详解
- 【转】C++异常中的堆栈跟踪
- POJ 1458 Common Subsequence
- 切勿自暴自弃
- MSSql存储过程高效应用
- hackintosh手动修复权限、重建缓存
- 关于android Ip地址的获取
- java synchronized详解
- U盘安装ubuntu12.04成功后系统无法启动的原因及解决办法
- linux简单之美(一)
- Citrix 排错 无法连结至citrix XenApp服务器,指定的地址未配置citrix xenapp服务器