HDU-1053:Advanced Fruits(LCS+路径保存)
2024-10-15 11:38:12
题意:将两个字符串合成一个串,不改变原串的相对顺序,可将相同字母合成一个,求合成后最短的字符串。
题解:LCS有三种状态转移方式,将每个点的状态转移方式记录下来,再回溯。
#include <bits/stdc++.h>
using namespace std; const double EPS = 1e-;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
const int maxn = + ;
char s[maxn], t[maxn];
int slen, tlen;
int dp[maxn][maxn], p[maxn][maxn]; void LCS()
{
memset(dp, , sizeof(dp));
for(int i = ; i <= slen; i++) p[i][] = ;
for(int i = ; i <= tlen; i++) p[][i] = -; for(int i = ; i <= slen; i++){
for(int j = ; j <= tlen; j++){
if(s[i] == t[j]){
dp[i][j] = dp[i-][j-] + ;
p[i][j] = ;
}
else if(dp[i-][j] >= dp[i][j-]){
dp[i][j] = dp[i-][j];
p[i][j] = ;
}
else{
dp[i][j] = dp[i][j-];
p[i][j] = -;
}
}
}
} void PrintLcs(int i, int j)
{
if(!i && !j) return ;
if(p[i][j] == ){
PrintLcs(i-, j-);
putchar(s[i]);
}
else if(p[i][j] == ){
PrintLcs(i-, j);
putchar(s[i]);
}
else if(p[i][j] == -){
PrintLcs(i, j-);
putchar(t[j]);
}
} int main()
{
while(scanf("%s%s", s + , t + ) != EOF){
slen = strlen(s + );
tlen = strlen(t + ); LCS();
PrintLcs(slen, tlen); puts("");
} return ;
}
最新文章
- C#客户端的异步操作
- Glide.js:响应式 &; 触摸友好的 jQuery 滑块插件
- JavaScript求和
- linux下开发c++第二弹--helloworld与makefile
- 问题-WIN7 ..\Bin\InitCC32.exe";.进程无法访问(拒绝访问)
- android设置图片变化的四种效果代码
- eclipse导出附带源码的jar包
- Spring+Redis(keyspace notification)实现定时任务(订单过期自动关闭)
- selenium 之 ActionChains (二)
- DIN(Deep Interest Network of CTR) [Paper笔记]
- Java学习笔记4(方法)
- Java 博客导航
- apache和nginx结合使用
- url两次编码
- Win7系统system进程句柄数一直增加解决方案
- unix下命令窗分屏工具
- python记录_day15 面向对象初识
- java 标准异常
- python queue和生产者和消费者模型
- atitit.javascript调用java in swt attilax 总结
热门文章
- ASP.Net GridView 基础 Template模板
- [LuoguP4711]分子质量(小模拟+玛丽题)
- flink统计根据账号每30秒 金额的平均值
- ./redis-trib.rb [ERR] Sorry, can&#39;t connect to node 192.168.*.*
- HTML5与CSS3网页设计
- C# 自定义特性Attribute
- Java常用修饰符总结
- 【css】 如何修改select的样式
- yyy loves Maths VII(状压DP)
- Windows Oracle连接ORA-12541:TNS:无监听程序