hdoj-1503 (LCS解的输出)
题目链接
回溯输出解
#include <bits/stdc++.h>
using namespace std;
const int N=;
int dp[N][N],dir[N][N];
char s1[N],s2[N];
int n1,n2;
void m_printf (int x,int y) {
if (x<=||y<=) {
for (int i=;i<=x;i++)
putchar(s1[i]);
for (int i=;i<=y;i++)
putchar(s2[i]);
return ;
}
if (dir[x][y]==) {
m_printf(x-,y-);
putchar (s1[x]);
}
else if (dir[x][y]>) {
m_printf(x-,y);
putchar (s1[x]);
}
else {
m_printf(x,y-);
putchar (s2[y]);
}
return ;
}
int main ()
{
while (~scanf (" %s %s",s1+,s2+)) {
memset (dp,,sizeof(dp));
int n1=strlen(s1+);
int n2=strlen(s2+);
for (int i=;i<=n1;i++)
for (int j=;j<=n2;j++) {
if (s1[i]==s2[j]) {
dir[i][j]=;
dp[i][j]=dp[i-][j-]+;
}
else if (dp[i-][j]>dp[i][j-]) {
dir[i][j]=;
dp[i][j]=dp[i-][j];
}
else {
dir[i][j]=-;
dp[i][j]=dp[i][j-];
}
}
// printf("%d\n",dp[n1][n2]);
m_printf(n1,n2);
printf("\n");
}
return ;
}
hdoj-1503 (LCS解的输出)的更多相关文章
- hdu 1503 LCS输出路径【dp】
hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左 ...
- hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏
a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...
- Advanced Fruits(HDU 1503 LCS变形)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 算法进阶面试题01——KMP算法详解、输出含两次原子串的最短串、判断T1是否包含T2子树、Manacher算法详解、使字符串成为最短回文串
1.KMP算法详解与应用 子序列:可以连续可以不连续. 子数组/串:要连续 暴力方法:逐个位置比对. KMP:让前面的,指导后面. 概念建设: d的最长前缀与最长后缀的匹配长度为3.(前缀不能到最后一 ...
- POJ 2250 (LCS,经典输出LCS序列 dfs)
题目链接: http://poj.org/problem?id=2250 Compromise Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- HDOJ 2016 数据的交换输出
Problem Description 输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数. Input 输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测 ...
- 51Nod-1006【LCS】+【输出路径】模板题
题目链接:https://vjudge.net/contest/225715#problem/B 转载于>>> 题目大意: 给出两个序列,要求输出它们的最长公共子序列. 解题思路: ...
- 详解JAVA输出Hello World
想必大家对这一段JAVA代码一定不会陌生: public class Test { public static void main(String[] args) { System.out.printl ...
- linux基础进阶命令详解(输出重定向(2>&1,1>&2,&>file)、输入重定向、管道符、通配符、三种引号、软连接、硬链接、根“/”、绝对路径vs相对路径)
本章命令(共9个): 1 2 3 4 5 6 7 8 9 输出重定向 输入重定向 管道符 通配符 三种引号 软连接 硬链接 根"/" 绝对路径vs相对路径 1.输出重定向 作用:一 ...
随机推荐
- Python获取时间戳
import datetime as dt dt.datetime.now().microsecond
- hibernate配置log
hibernate依赖jboss-logging,通过它选择对应的对应的日志包,选择的逻辑课查看具体代码org.jboss.logging.LoggerProviders. 先通过系统变量(org.j ...
- python2.x 与 python3.x的区别
从语言的源码角度: python2.x 的源码书写不够规范,且源码有重复,代码的复用率不高; python3.x 的源码清晰.优美.简单 从语言的特性角度: python2.x 默认为ASCII字符编 ...
- 【原创】QT简单计算器
代码 //main.cpp #include "calculator_111.h" #include <QtWidgets/QApplication> int main ...
- python全栈开发笔记---基本数据类型--数字型魔法
数字 int a1 =123 a2=456 int 讲字符串转换为数字 a = " #字符串 b = int(a) #将字符串转换成整形 b = b + 1000 #只有整形的时候才可以进 ...
- ActiveMQ的P2P示例
ActiveMQ的P2P示例(点对点通信) (1)下载安装activemq,启动activeMQ. 详细步骤参考博客:http://www.cnblogs.com/DFX339/p/9050878.h ...
- 当你有双网络(内部网+互联网)时,如何透明NAT给其他电脑上网。虚拟机+爱快
一:简介 具体环境是这样的:单位没有提供互联网连接,都是内部网,linux服务器,无法连接源更新,docker无法pull镜像,python无法在线pip安装包. 真是郁闷到想死啊. 好在我的笔记本有 ...
- VSTO:使用C#开发Excel、Word【4】
<Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath >——By Eric C ...
- js call回调的this指向问题
function fn1(){ console.log(1); } function fn2(){ console.log(2); } fn1.call(fn2); //输出 1 fn1.call.c ...
- 7 Serial Configuration 理解(二)
*Serial Configuration Mode 串行配置模式分为:Master Serial 和 Slave Serial (如下图)两类: 两者的区别在与CCLK的输入输出方向:主动模式下为输 ...