链接:HDU-1053:Advanced Fruits

题意:将两个字符串合成一个串,不改变原串的相对顺序,可将相同字母合成一个,求合成后最短的字符串。

题解: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 ;
}

最新文章

  1. C#客户端的异步操作
  2. Glide.js:响应式 &amp; 触摸友好的 jQuery 滑块插件
  3. JavaScript求和
  4. linux下开发c++第二弹--helloworld与makefile
  5. 问题-WIN7 ..\Bin\InitCC32.exe&quot;.进程无法访问(拒绝访问)
  6. android设置图片变化的四种效果代码
  7. eclipse导出附带源码的jar包
  8. Spring+Redis(keyspace notification)实现定时任务(订单过期自动关闭)
  9. selenium 之 ActionChains (二)
  10. DIN(Deep Interest Network of CTR) [Paper笔记]
  11. Java学习笔记4(方法)
  12. Java 博客导航
  13. apache和nginx结合使用
  14. url两次编码
  15. Win7系统system进程句柄数一直增加解决方案
  16. unix下命令窗分屏工具
  17. python记录_day15 面向对象初识
  18. java 标准异常
  19. python queue和生产者和消费者模型
  20. atitit.javascript调用java in swt attilax 总结

热门文章

  1. ASP.Net GridView 基础 Template模板
  2. [LuoguP4711]分子质量(小模拟+玛丽题)
  3. flink统计根据账号每30秒 金额的平均值
  4. ./redis-trib.rb [ERR] Sorry, can&#39;t connect to node 192.168.*.*
  5. HTML5与CSS3网页设计
  6. C# 自定义特性Attribute
  7. Java常用修饰符总结
  8. 【css】 如何修改select的样式
  9. yyy loves Maths VII(状压DP)
  10. Windows Oracle连接ORA-12541:TNS:无监听程序