代码语言:javascript 代码运行次数:0 运行 AI代码解释 #include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;constint maxn=1000+5;int n,m;int dp[maxn][maxn];char s1[maxn],s2[maxn];intmain(){while(scanf("%s%s",s1,s2)==2){n=strlen(s1);//s1串长...
POJ-1458 LCS(线性动态规划) 此题经典线性动态规划。 代码如下: #include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>usingnamespacestd;constintmaxn=505;intd[maxn][maxn];intmain(void){stringa,b;while(cin>>a>>b){ memset(d,0,sizeof(d));for(inti=1;i<=a...
所以从ans[][]右下角逆推即可求出temp[ ],然后逆序输出temp[]即可。代码如下: 1//51Nod动态规划教程例题 求最长公共子序列的长度并输出一个最长公共子序列2#include <iostream>3#include <cstring>4usingnamespacestd;5#definemaxN 50056charsz1[maxN];7charsz2[maxN];8intmaxLen[2][maxN];9charans[...
HDU 1159 && POJ 1458 最长公共子序列。状态转移方程见代码。 #include <iostream> #include <cstdio> #include <cstring> using namespace std; char s1[1005],s2[1005]; int dp[1005][1005]; int main() { while(scanf("%s",s1+1)!=EOF) { scanf("%s",s2+1); memset(dp,0,sizeof(dp)); ...
如果选择第i个物品,那么dp[i][j] = dp[i-1][j-C[i]] + V,因为选择了i,剩余容量只剩下j-C[i],价值+V。 这个方程算是背包问题里面最容易理解的。 接着需要理解如果从二维数组变成一维数组。 因为在迭代过程中,把i的阶段分为奇数和偶数2部分,每一次交替过程中,奇数部分会用到偶数部分的值,偶数部分...
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 ...
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...
delete[] c;53returnlen;54}55voidPrintLCS(int**b,char*str1,inti,intj)56{57if(i==0|| j==0)58return;59if(b[i][j]==0)60{61PrintLCS(b, str1, i-1, j-1);//从后面开始递归,所以要先递归到子串的前面,然后从前往后开始输出子串62printf("%c",str1[i-1]);//c[][]的第i行元素...