hdu 1503 最长公共子序列
2024-09-04 17:48:50
/*
给两个串a,b。输出一个最短的串(含等于a的子序列且含等于b的子序列)
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn=;
int dp[maxn][maxn],path[maxn][maxn];
int len,len1,len2;
char text[maxn],a[maxn],b[maxn]; void getpath()
{
int n=len,i=len1,j=len2;
while(n)
{
if(path[i][j]==)
text[n--]=a[i],i--,j--;
else if(path[i][j]==) i--;
else j--;
}
} int main()
{
int i,j,k;
a[]=b[]='';
while(~scanf("%s%s",a+,b+))
{
memset(dp,,sizeof(dp));
len1=strlen(a)-,len2=strlen(b)-;
for(i=;i<=len1;i++)
{
for(j=;j<=len2;j++)
{
if(a[i]==b[j])dp[i][j]=dp[i-][j-]+,path[i][j]=;
else
{
if(dp[i-][j]>dp[i][j-]) dp[i][j]=dp[i-][j],path[i][j]=;
else dp[i][j]=dp[i][j-],path[i][j]=;
}
}
}
len=dp[len1][len2];
getpath();
j=,k=;
for(i=;i<=len;i++)
{
while(a[j]!=text[i]) printf("%c",a[j++]);
while(b[k]!=text[i]) printf("%c",b[k++]);
printf("%c",text[i]);
j++;k++;
}
while(j<=len1) printf("%c",a[j++]);
while(k<=len2) printf("%c",b[k++]);
printf("\n");
}
return ;
}
最新文章
- [Android]使用Dagger 2依赖注入 - API(翻译)
- [LeetCode] Rank Scores 分数排行
- Node.js学习之简介
- .net网站能走多远
- HTTP权威指南笔记-2.URL与资源
- Hbase写入hdfs源码分析
- json 特殊字符 javascript 特殊字符处理(转载)
- opencv的一次性配置
- jquery Ajax应用总结
- CSS动画:Transform中使用频繁的scale,rotate,translate动画
- 转:PHP 使用ZipArchive压缩文件并下载
- ProgressDialog(四)——更改系统自带ProgressDialog文字大小
- Git建空白分支
- <;c:forEach var=";role"; items=";[entity.Role@d54d4d, entity.Role@1c61868, entity.Role@6c58db, entity.Role@13da8a5]";>; list 集合数据转换异常
- git 入门与应用
- Latex常用软件
- 四、Sql Server 基础培训《进度4-插入数据(实际操作)》
- Java基本语法---个人参考资料
- C#开发Unity游戏教程之使用脚本变量
- yum使用过程中的常见错误