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 ...
随机推荐
- DOS系统里,分屏显示目录的命令是什么??
dir /sdir /pdir /w 我记得这三个都是我当年常用的命令,有分瓶的,有滚动时候每页停顿的,还有去掉详细信息的吧,, 可以放在一起使用.如dir /p/w /p是滚动时候中间停顿的,/w是 ...
- 几个.net的GUI控件库
https://github.com/firstfloorsoftware/mui http://wpftoolkit.codeplex.com/ https://github.com/fluentr ...
- nutch 采集到的数据与实际不符
现象,这个网站我总计能抽取将近500个URL,但实际只抽取了100条 解析:nutch默认从一个页面解析出的链接,只取前 100 个. <property> <name>db. ...
- C#读取Excel五种方式的体会
原地址: http://blog.csdn.net/dapengbusi/article/details/38117817 http://blog.csdn.net/dapengbusi/articl ...
- ubuntu使用中的一些问题
ubuntu如何修改计算机名? sudo gedit /etc/hostname 如何查看ubuntu操作系统位数 方法1: #查看long的位数,返回32或64 getconf LONG_BIT 方 ...
- Qt中的多线程编程
http://www.ibm.com/developerworks/cn/linux/l-qt-mthrd/ Qt 作为一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造图形用户界面的强大功 ...
- BIOS与CMOS有什么区别
本文介绍BIOS与CMOS区别,BIOS是什么?BIOS全称Basic Input/Output System,所以BIOS本身个是系统简称,所以我们常说的BIOS芯片确切的讲是写有BIOS系统的芯片 ...
- char 和 varchar
固定长度或可变长度的字符数据类型. char [ ( n ) ] 固定长度,非 Unicode 字符数据,长度为 n 个字节.n 的取值范围为 1 至 8,000,存储大小是 n 个字节.char 的 ...
- 我的第一个Spring程序
1.程序结构 2.各个文件 ByeService.java package com.service; public class ByeService { private String name; pu ...
- a++与=++a的区别
//a++;//a=a+1; // ++a;//a=a+1; //Console.WriteLine(a++);// Console.WriteL ...