题意:给你两个单词序列,求出他们的最长公共子序列。

多组数据输入,单词序列长度<=100,单词长度<=30

因为所有组成LCS的单词都是通过 a[i] == b[j] 更新的。

打印序列的时候用mark标记一下,然后回溯找就可以了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>
#include <cstring>
#include <string>
using namespace std; #define maxn 150 vector<string> a, b, ans;
int f[maxn][maxn], mark[maxn][maxn]; void printString(int i, int j)
{
if (i < || j < )
return; if (mark[i][j] == )
{
ans.push_back(a[i]);
printString(i-, j-);
}
else if (mark[i][j] == )
{
printString(i, j-);
}
else
printString(i-, j);
} int main()
{
string s;
while(cin >> s)
{
a.clear(); b.clear(); a.push_back(s);
string t;
while(cin >> t)
{
if (t == "#") break;
a.push_back(t);
} while(cin >> t)
{
if (t == "#") break;
b.push_back(t);
} int id = , n = a.size(), m = b.size();
memset(f, , sizeof(f));
memset(mark, , sizeof(mark)); for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
if (a[i] == b[j])
{
f[i][j] = f[i-][j-]+;
mark[i][j] = ;
}
else if (f[i-][j] > f[i][j-])
{
f[i][j] = f[i-][j];
mark[i][j] = -;
}
else
{
f[i][j] = f[i][j-];
mark[i][j] = ;
} ans.clear();
printString(n-, m-); for (int i = ans.size()-; i > ; i--)
cout << ans[i] << " "; cout << ans[] << endl;
}
}

POJ - 2250 Compromise (LCS打印序列)的更多相关文章

  1. POJ 2250 Compromise(LCS)

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

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

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

  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【LCS】+输出路径

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

  5. poj 2250 Compromise(区间dp)

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

  6. POJ 2250 Compromise (UVA 531)

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

  7. POJ2250 - Compromise(LCS+打印路径)

    题目大意 给定两段文本,问公共单词有多少个 题解 裸LCS... 代码: #include<iostream> #include<string> using namespace ...

  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 ...

随机推荐

  1. 洛谷P1965 转圈游戏

    https://www.luogu.org/problem/show?pid=1965 快速幂 #include<iostream> #include<cstdio> #inc ...

  2. .net memcache

    非常感谢csdn及冷月宫主让我很快学会了.net操作 memcache 文章转自:http://download.csdn.net/detail/e_wsq/4358982 C#存取Memcache的 ...

  3. ThreadLocal(关于struts2的ThreadLocal,实际上Jdk1.2就有了)

    ThreadLocal是通过在不同线程中操作变量的副本,来达到线程安全的目的,是用空间资源换时间资源的方式.今天在看struts2源码的时候,发现ActionContext中,就持有一个静态的Thre ...

  4. Kendo UI 单页面应用(三) View

    Kendo UI 单页面应用(三) View view 为屏幕上某个可视部分,可以处理用户事件. View 可以通过 HTML 创建或是通过 script 元素.缺省情况下 View 将其所包含的内容 ...

  5. redis 知识归档

    中文版redis命令 http://www.redis.net.cn/order/    redis例子 https://github.com/ServiceStack/ServiceStack.Ex ...

  6. AD 域复制FRS 迁移到DFSR

    假设您尝试将在先前版本的Windows Server上运行的某个Active Directory域控制器(DC)升级到Windows Server 2019. 您可能会看到以下错误: “副本验证失败. ...

  7. 《学习CSS布局》学习笔记

    近几天做了一个小的企业展示网站.虽然页面是在模板的基础上改的,但改的多了不熟悉CSS也很麻烦.正好我看到了学习CSS布局这个网站,于是补习了一下CSS知识. CSS的显示 CSS的元素分为两类:块级元 ...

  8. 用dfs求解八皇后问题

    相信大家都已经很熟悉八皇后问题了,就是指:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法.主要思路:按行进行深度优先搜索,在该 ...

  9. PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep

    PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...

  10. Luogu P1666 前缀单词

    校内资格赛题目,差点高一就要\(\tt{AFO}\)了 30分思路 对30%的数据,满足$1≤n≤10 $ 所以我们可以子集枚举,实际得分40pts #include<iostream> ...