poj 1458最长公共子序列

xiaoxiao2021-02-27  180

#include<cstdio> #include<cstring> #define MAX(x,y) ((x)>(y)?(x):(y)) int dp[1000][1000]; int main() { char str1[1000],str2[1000]; while(~scanf("%s%s",str1,str2)) { int len1=strlen(str1); int len2=strlen(str2); memset(dp,0,sizeof(dp)); for(int i=0;i<len1;i++) { for(int j=0;j<len2;j++) { if(str2[j]==str1[i]) { if(i>0&&j>0) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=1; } else { if(i>0&&j>0) dp[i][j]=MAX(dp[i-1][j],dp[i][j-1]); else if(i>0) dp[i][j]=dp[i-1][j]; else if(j>0) dp[i][j]=dp[i][j-1]; } } } printf("%d\n",dp[len1-1][len2-1]); } }
转载请注明原文地址: https://www.6miu.com/read-13698.html

最新回复(0)