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 ...
随机推荐
- hdu 2594 Simpsons’ Hidden Talents KMP应用
Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...
- 分享零基础学习Hadoop方法
(我不是Hadoop专家,也只是一个初学者,这里我也只是就自己的学习体会,站在初学者的角度谈一下如何入门.) 首先我觉得应该思考这样一个问题:Hadoop对于我们来讲,是一种工具,那么Hadoop帮助 ...
- PL/SQL — BULK COLLECT用法
BULK COLLECT 子句会批量检索结果,即一次性将结果集绑定到一个集合变量中,并从SQL引擎发送到PL/SQL引擎.通常可以在SELECT INTO.FETCH INTO以及RETURNING ...
- C语言小知识
1.数组初始化 a[5] = {1}; //1,0,0,0,0 a[5] = {0}; //0,0,0,0,0 a[3][3] = {1}; //1,0,0;0,0,0;0,0,0 a[3][ ...
- 产生一个长度为100的int数组,并向其中随机插入1-100,不能重复
]; ArrayList myList=new ArrayList(); Random rnd=new Random(); ) { ,); if(!myList.Contains(num)) myLi ...
- 使用代理下载Android SDK
hx.gy:1080是红杏公益服务器,方便科学工作者访问一些常用的科学网站. 在Android SDK Manager中选择[Tools]->[Options],如下设置: HTTP Proxy ...
- call()和apply()的区别
var a = function(a,b){ console.log(a+b); }, b = { c:5, d:3 }; a.call(b,1,2); a.apply(b,[1,2]); a.cal ...
- jquery and event
jquery阻止事件冒泡 event.stopPropagation(); event.cancelBubble = true; jquery阻止默认操作 event.preventDefault() ...
- 查看java的.class文件的方法
在不通过eclipse等IDE安装反编译插件的情况下查看java的.class文件的方法:可以通过下载jd-gui class文件查看工具进行查看.如附件的“jd-gui.exe”程序. 1. 从网上 ...
- UVALive - 5135 Mining Your Own Business
刘汝佳白书上面的一道题目:题意是给定一个联通分量,求出割顶以及双连通分量的个数,并且要求出安放安全井的种类数,也就是每个双连通分量中结点数(除开 割顶)个数相乘,对于有2个及以上割顶的双连通分量可以不 ...