您的位置:首页 > 其它

最长公共子序列

2016-08-26 11:10 323 查看
/*************************************************
*@time          2016/08/25 9:48
*@place         ctrip.15#.9f
**************************************************/
#include<cstdio>
#include<cstring>
#define MAX_LENGTH 100

int main()
{
char str1[]="123456789";
char str2[]="123";
int dp[10][4];
for(int i=0;i<=9;i++)
{
for(int j=0;j<=3;j++)
{
dp[i][j]=0;
}
}
for(int i=1;i<=9;i++)
{
for(int j=1;j<=3;j++)
{
if(str1[i-1]==str2[j-1])
{
dp[i][j]=dp[i-1][j-1]+1;
}
else if(dp[i-1][j]>dp[i][j-1])
{
dp[i][j]=dp[i-1][j];
}
else
{
dp[i][j]=dp[i][j-1];
}
}
}
printf("%s %s\n",str1,str2);
for(int i=9,j=3;i>0&&j>0;)
{

if(str1[i-1]==str2[j-1])
{
printf("%c ",str1[i-1]);
i--;
j--;
}
else if(dp[i-1][j]>dp[i][j-1])
{
i--;
}
else
{
j--;
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: