POJ2250 - Compromise(LCS+打印路径)
题目大意
给定两段文本,问公共单词有多少个
题解
裸LCS。。。
代码:
#include<iostream>
#include<string>
using namespace std;
#define MAXN 105
string x[MAXN],y[MAXN];
int path[MAXN][MAXN],dp[MAXN][MAXN];
int n,m;
void work()
{
for(int i=1; i<=n; i++)
dp[i][0]=0;
for(int j=0; j<=m; j++)
dp[0][j]=0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(x[i]==y[j])
{
dp[i+1][j+1]=dp[i][j]+1;
path[i+1][j+1]=0;
}
else
{
if(dp[i][j+1]<dp[i+1][j])
{
dp[i+1][j+1]=dp[i+1][j];
path[i+1][j+1]=1;
}
else
{
dp[i+1][j+1]=dp[i][j+1];
path[i+1][j+1]=-1;
}
}
}
void print(int i,int j)
{
if(i==0||j==0)
return;
if(path[i][j]==0)
{
print(i-1,j-1);
cout<<x[i-1]<<" ";
}
else if(path[i][j]==1)
print(i,j-1);
else
print(i-1,j);
}
int main()
{
string s;
while(cin>>s)
{
n=0,m=0;
x[n++]=s;
while(cin>>s&&s[0]!='#')
x[n++]=s;
while(cin>>s&&s[0]!='#')
y[m++]=s;
work();
print(n,m);
cout<<endl;
}
return 0;
}
POJ2250 - Compromise(LCS+打印路径)的更多相关文章
- UVA 531 - Compromise(dp + LCS打印路径)
Compromise In a few months the European Currency Union will become a reality. However, to join th ...
- LCS(打印路径) POJ 2250 Compromise
题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] ...
- 最长公共子序列Lcs(打印路径)
给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是,abca也是,其中abca是这 ...
- LCS(打印全路径) POJ 2264 Advanced Fruits
题目传送门 题意:两个字符串结合起来,公共的字符只输出一次 分析:LCS,记录每个字符的路径 代码: /* LCS(记录路径)模板题: 用递归打印路径:) */ #include <cstdio ...
- LCS与打印路径
/* LCS */ #include<bits/stdc++.h> using namespace std; const int maxn = 1000; int dp[maxn][max ...
- hdu 1503 Advanced Fruits(LCS输出路径)
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- UVA 624 (0 1背包 + 打印路径)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...
- UVA 10054 The Necklace(欧拉回路,打印路径)
题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- zoj 3088 Easter Holidays(最长路+最短路+打印路径)
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...
随机推荐
- typedef和#define的区别
转自:http://www.cnblogs.com/kerwinshaw/archive/2009/02/02/1382428.html 一.typedef的用法在C/C++语言中,typedef常用 ...
- 一步步学习ASP.NET MVC3 (3)——Razor(1)
请注明转载地址:http://www.cnblogs.com/arhat 首先这个<一步步学习ASP.NET MVC3>前段时间有些忙,没有顾得上写文章,昨天呢写了3个和ASP.NET的相 ...
- Extjs4.2 多选下拉框
//多选下拉框 Ext.define('MDM.view.custom.MultiComboBox', { extend: 'Ext.form.ComboBox', alias: 'widget.mu ...
- spring ioc aop 原理
spring ioc aop 原理 spring ioc aop 的原理 spring的IoC容器是spring的核心,spring AOP是spring框架的重要组成部分. 在传统的程序设计中,当调 ...
- use worker without js file
var blob = new Blob(['onmessage=function(e){postMessage(e.data);}']); debugger; // Obtain a blob URL ...
- RPM是RedHat Package Manager(RedHat软件包管理工具)
RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...
- ***PHP请求服务curl以及json的解析
对于thinkphp框架,相信每一个php开发者都会有了解或者熟悉吧!前端很多都用的ajax的结合,前几天遇到了一个问题,就是请求另一个服务,也就是请求一个接口,然后对方返回一个json串,一开始对c ...
- Vmware 8.00 文件共享ubuntu
http://bolg.sinaapp.com/html/2012/1848.html 这是解决vm不能共享的解决方案. 今天学会的Linux命令: cp -i *** ~/tmp cd VMware ...
- JavaScript 判断是否为undefined
if (typeof(reValue) == "undefined") { alert("undefined"); }
- UVA 10651 Pebble Solitaire 状态压缩dp
一开始还在纠结怎么表示一个状态,毕竟是一个串.后来搜了一下题解发现了这里用一个整数的前12位表示转态就好了 ,1~o,0~'-',每个状态用一个数来表示,然后dp写起来就比较方便了. 代码: #inc ...