题意是找两篇文章中的最长子单词序列

能得出个数,但不知如何输出,找不到路径

看了别人的dfs,有所领悟:

若输入s1:ab,bd,fk,ce,ak,bt,cv

s2: ab,fk,ce,tt,ak,bt,深搜路径数字涂红
dp棋盘如下:
           ab   fk   ce   tt   ak   bt  <-s2

0        0        0        0       0        0

ab         0               1        1    1    1    1        输出 ab

bd   0       1    1     1    1    1

fk    0   1        2     2    2    2        输出 fk

ce   0    1    2            3    3         输出 ce

ak   0    1    2    3    3        4         输出 ak

bt   0    1     2    3    3    4    5         输出 bt

cv   0    1    2     3    3    4  

^
 s1

代码如下:

#include<stdio.h>
#include<string.h>
int dp[][];
char s1[][],s2[][];
int max(int x,int y)
{
return x>y?x:y;
}
int dfs(int a,int b)
{
if(dp[a][b]==)
return ;
if(dp[a][b]==dp[a-][b-]+&&strcmp(s1[a],s2[b])==)
{
dfs(a-,b-);
if(dp[a][b]==)
printf("%s",s1[a]);
else
printf(" %s",s1[a]);
}
else
if(dp[a][b]==dp[a-][b])
dfs(a-,b);
else
dfs(a,b-);
return ;
}
int main()
{
int i,j;
int L1,L2;
while(scanf("%s",&s1[])!=EOF) //读到文件结束标志停止,下标为几就是第几个单词,便于输出
{
L1=;L2=; //L1为第一篇文章单词个数,L2为第二篇
while(scanf("%s",&s1[++L1])==)
if(strcmp(s1[L1],"#")==) //读到 # 本篇文章结束
{
L1--;
break;
}
while(scanf("%s",&s2[++L2])==)
if(strcmp(s2[L2],"#")==)
{
L2--;
break;
}
memset(dp,,sizeof(dp));
for(i=; i<=L1; i++) //初始化dp棋盘
for(j=; j<=L2; j++)
if(strcmp(s1[i],s2[j])==)
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i-][j],dp[i][j-]);
dfs(L1,L2); //从右下角开始搜
printf("\n");
}
return ;
}

Poj-2250-Compromise的更多相关文章

  1. POJ 2250 Compromise(LCS)

    POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...

  2. poj 2250 Compromise(区间dp)

    题目链接:http://poj.org/problem?id=2250 思路分析:最长公共子序列问题的变形,只是把字符变成了字符串,按照最长公共子序列的思路即可以求解. 代码如下: #include ...

  3. LCS(打印路径) POJ 2250 Compromise

    题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] ...

  4. POJ 2250 Compromise (UVA 531)

    LCS问题.基金会DP. 我很伤心WA非常多.就在LCS问题,需要记录什么路. 反正自己的纪录path错误,最后,就容易上当. 没有优化,二维阵列,递归打印,cin.eof() 来识别 end of ...

  5. POJ 2250 Compromise【LCS】+输出路径

    题目链接:https://vjudge.net/problem/POJ-2250 题目大意:给出n组case,每组case由两部分组成,分别包含若干个单词,都以“#”当结束标志,要求输出最长子序列. ...

  6. POJ - 2250 Compromise (LCS打印序列)

    题意:给你两个单词序列,求出他们的最长公共子序列. 多组数据输入,单词序列长度<=100,单词长度<=30 因为所有组成LCS的单词都是通过 a[i] == b[j] 更新的. 打印序列的 ...

  7. POJ 2250 (LCS,经典输出LCS序列 dfs)

    题目链接: http://poj.org/problem?id=2250 Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  8. 【POJ 2250】Compromise(最长公共子序列LCS)

    题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...

  9. POJ 2250(LCS最长公共子序列)

    compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  10. POJ 2250(最长公共子序列 变形)

    Description In a few months the European Currency Union will become a reality. However, to join the ...

随机推荐

  1. JS生成雪花

    <script type="text/javascript"> (function(){ function snow(left,height,src){ var div ...

  2. PIC32MZ tutorial -- 32-bit Timer

    The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has fou ...

  3. regular expressions

  4. [转]DCM Tutorial – An Introduction to Orientation Kinematics

    原地址http://www.starlino.com/dcm_tutorial.html Introduction This article is a continuation of my IMU G ...

  5. mysql的从头到脚优化之数据库引擎的选择(转载)

    一. Mysql常用的存储引擎包括Innodb和Myisam以及memory引擎,但是最常用的莫过于Innodb引擎和MyISAM引擎,下边分别做下记录和比较: 下面思考下这几个问题: 你的数据库需要 ...

  6. MFC的BeginWaitCursor和EndWaitCursor函数

    MFC提供了BeginWaitCursor和EndWaitCursor函数来显示和隐藏等待的图标,以下是例子. void CMainView::OnEditClone() {     BeginWai ...

  7. asp.net web api 测试帮助页面建立并测试

    asp.net web api 测试帮助页面建立并测试 现在使用WEB API来开发,越来越流行. 在开发过程中的测试调试,可以使用Fiddler等工具来帮助测试外,还有: 在asp.net 中有种方 ...

  8. mysql 查询去除空格字符然后倒入新表

    /* 导入数据的时候,有空白字符,去除一下然后导出. 四年前干的事,现在再干一遍. */ $dbhost= '127.0.0.1'; $dbuser= 'root'; $dbpass= '123456 ...

  9. 关于c#中的console用法大全

    C#之Console   Console.Write  表示向控制台直接写入字符串,不进行换行,可继续接着前面的字符写入.Console.WriteLine  表示向控制台写入字符串后换行.Conso ...

  10. VS&SQL StartUp Crash - CLR20R3

    VS2013和SQL Management Studio 在启动时直接崩溃,错误提示CLR20R3,问题签名4是windowsbase, 这说明是操作系统的问题导致启动崩溃,在网上找到一些解决方案: ...