Common Subsequence (dp)
来源:互联网 发布:投影机融合软件 编辑:程序博客网 时间:2024/06/11 12:51
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.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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.
Input
abcfbc abfcab
programming contest
abcd mnp
Output
4
2
0
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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.
Input
abcfbc abfcab
programming contest
abcd mnp
Output
4
2
0
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
最大公共子序列。dp[i][j]表示前i个字符与前j个字符相同的个数
AC代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#include<string>#include<queue>#include<set>#include<vector>#include<map>#include<stack>#include<cstdlib>using namespace std;typedef long long ll;char a[1005],b[1005];int dp[1005][1055];int main(){ int ans; while(scanf("%s%s",a,b)!=EOF){ memset(dp,0,sizeof(dp)); int la=strlen(a); int lb=strlen(b);// printf("%d %d\n",la,lb); for(int i=0;i<la;i++) dp[i][0]=0; for(int i=0;i<lb;i++) dp[0][i]=0; for(int i=1;i<=la;i++) for(int j=1;j<=lb;j++){ if(a[i-1]==b[j-1])dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);// printf("%d ",ans); } printf("%d\n",dp[la][lb]); } return 0;}
阅读全文
0 0
- poj1458 dp Common Subsequence
- Common Subsequence (dp)
- Common Subsequence HDU dp
- Common Subsequence(DP)
- hdu1159 Common Subsequence DP
- POJ1458 Common Subsequence DP
- [DP]Longest Common Subsequence
- HDOJ1159 Common Subsequence(dp)
- hdu1159 Common Subsequence--DP
- DP-Common Subsequence
- Common Subsequence (dp)
- Common Subsequence [dp]
- Common Subsequence (dp)
- Common Subsequence dp
- zoj 1733 Common Subsequence dp
- ZOJ 1733 Common Subsequence【DP】
- hdu 1159 Common Subsequence #DP
- hdu 1159 Common Subsequence (dp)
- smartctl命令详解
- ReactNative-ListView
- 语音传输 音频采集
- 实战:战狼2票房数据分析——(3)数据读取及分析
- HDU.2795 Billboard
- Common Subsequence (dp)
- WebView缩放后左边内容被遮盖的问题
- 互斥量和条件变量解决读者-写者问题
- mcp2515带spi的can驱动移植总结
- 1013. 数素数 (20)
- java之dbcp笔记
- Android 应用的启动过程
- 警察智力训练--蓝桥杯历年java真题(java递归实现eval算法)
- UVA 1605 Building for UN