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 ...
随机推荐
- [转]python 之字典{}(Hashmap)
字典 python里的字典就像java里的HashMap,以键值对的方式存在并操作,其特点如下 通过键来存取,而非偏移量: 键值对是无序的: 键和值可以是任意对象: 长度可变,任意嵌套: 在字典里,不 ...
- merge into 和 update 的效率对比
以前只考虑 merge into 只是在特定场合下方便才使用的,今天才发现,merge into 竟然会比 update 在更新数据时有这么大的改进.其实呢,merge into部分的update和u ...
- 关于JS中的apply()与call()使用方法与区别
Js apply方法详解我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里 ...
- 下拉框点链接js
$("#input_text").click(function(){ $("#input_fonts").show(); }); $("#input_ ...
- 在C#中关于excel的导入和导出操作
一.先来看看最常见的导入操作吧! private void Import() { //打开excel选择框 OpenFileDialog frm = new OpenFileDialog(); frm ...
- SQLite入门与分析(八)---存储模型(3)
写在前面:接上一节,本节主要讨论索引页面格式,以及索引与查询优化的关系. (1)索引页面格式sqlite> select * from sqlite_master;table|episodes| ...
- get started with laravel
Browsing the API (http://laravel.com/api) can be somewhat intimidating at first.But it is often the ...
- java比较器Comparable接口和Comaprator接口
Comparable故名思意是比较,意思就是做比较的,然后进行排序. 1.什么是comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 ,类的 compar ...
- 【HDOJ】Power Stations
DLX.针对每个城市,每个城市可充电的区间构成一个plan.每个决策由N*D个时间及N个精确覆盖构成. /* 3663 */ #include <iostream> #include &l ...
- 【HDOJ】5564 Clarke and digits
DP+快速矩阵幂.注意base矩阵的初始化,不难. /* 5564 */ #include <iostream> #include <string> #include < ...