题目链接:https://vjudge.net/problem/POJ-2250

题目大意:
给出n组case,每组case由两部分组成,分别包含若干个单词,都以“#”当结束标志,要求输出最长子序列。

#include <iostream>
#include <string>
using namespace std; string a[], b[], ans[];
int alen, blen, len, dp[][], num[][]; void LCSLength() {
memset(dp, , sizeof(dp));
memset(num, , sizeof(num));
for (int i = ; i <= alen; i++) {
for (int j = ; j <= blen; j++) {
if (a[i] == b[j]) {
dp[i][j] = dp[i - ][j - ] + ;
num[i][j] = ;
}
else if (dp[i - ][j] >= dp[i][j - ]) {
dp[i][j] = dp[i - ][j];
num[i][j] = ;
}
else {
dp[i][j] = dp[i][j - ];
num[i][j] = ;
}
}
}
} void LCS(int i, int j) { //注意打印路径的方法
if (i == || j == ) return;
if (num[i][j] == ) {
ans[len--] = a[i];
LCS(i - , j - );
}
else if (num[i][j] == ) LCS(i - , j);
else LCS(i, j - );
} int main() { //注意输入格式
string s;
while (cin >> s) { //由于题目要求输入直到文件结束,所以这里要这样写
alen = , blen = ;
a[++alen] = s;
while (cin >> s) {
if (s=="#") break;
a[++alen] = s;
}
while (cin >> s) {
if (s=="#") break;
b[++blen] = s;
}
LCSLength();
len = dp[alen][blen];
LCS(alen, blen);
cout << ans[];
for (int i = ; i <= dp[alen][blen]; i++) cout << " " << ans[i];
cout << endl;
}
return ;
}

2018-05-18

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打印序列)

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

  5. poj 2250 Compromise(区间dp)

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

  6. hdu 1503 Advanced Fruits(LCS输出路径)

    Problem Description The company "21st Century Fruits" has specialized in creating new sort ...

  7. 迷宫问题 POJ - 3984 (搜索输出路径)

    题目大意 题目不需要大意,poj居然还有中文题 鸣谢 特别鸣谢ljc大佬提供的方法!!! 解法 我们可能输出个最短路径的长度比较简单,但是输出最短路径真的是没有做过,这里有一种简单的方法 因为我们的d ...

  8. hdu 1503 LCS输出路径【dp】

    hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左 ...

  9. Educational DP Contest F - LCS (LCS输出路径)

    题意:有两个字符串,求他们的最长公共子序列并输出. 题解:首先跑个LCS记录一下dp数组,然后根据dp数组来反着还原路径,只有当两个位置的字符相同时才输出. 代码: char s[N],t[N]; i ...

随机推荐

  1. The folder can’t be opened because you don’t have permission to see its contents.

    1 自己在windows上面copy过去的文件夹,在Mac下面无法查看 一开始以为是windows文件的权限问题,然后 自己赋予了everyone所有的权限,结果在Mac上面还是无法打开文件夹 2 最 ...

  2. JNI打通java和c

    1.JNI简介 The Java Native Interface (JNI) is a programming framework that enables Java code running in ...

  3. python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib

    python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...

  4. k-means 图像分割

    经典的无监督聚类算法,不多说,上代码. import numpy as np import pandas as pd import copy import matplotlib.pyplot as p ...

  5. 高级 Java 必须突破的 10 个知识点!

    1.Java基础技术体系.JVM内存分配.垃圾回收.类装载机制.性能优化.反射机制.多线程.网络编程.常用数据结构和相关算法. 2.对面向对象的软件开发思想有清晰的认识.熟悉掌握常用的设计模式. 3. ...

  6. 004_Nginx 499错误的原因及解决方法

    一. 今天进行系统维护,发现了大量的499错误, 499错误 ngx_string(ngx_http_error_495_page), /* 495, https certificate error ...

  7. 关于nginx报错/usr/share/nginx/html/jiankongshare" failed (2: No such file or directory)的问题解决

    nginx的location虚拟目录配置: monitor.conf server {       server_name   monitor.chinasoft.com;       server_ ...

  8. windows2008r2系统破解登录密码方法

    破解windows 2008 r2系统登录密码方法: 1.重启系统,使用windows2008r2安装光盘引导 按住shift+f10 2.切换到d:windows\system32目录(使用cmd. ...

  9. 为cobbler自动化安装系统工具添加epel源

    关于cobbler的安装及部署,参考:CentOS 6.5自动化运维之基于cobbler服务的自动化安装操作系统详解http://blog.csdn.net/reblue520/article/det ...

  10. Tpcc-MySQL对mysql数据库进行性能测试报告、分析及使用gnuplot生成图表展示

    TPC-C是专门针对联机交易处理系统(OLTP系统)的规范,一般情况下我们也把这类系统称为业务处理系统. tpcc-mysql是percona基于TPC-C(下面简写成TPCC)衍生出来的产品,专用于 ...