TOJ 1139.Compromise
2015-06-03
问题简述:
大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列。
简单的LCS问题,但是输入的是一段话了,而且公共部分比较是字符串的比较。
原题链接:http://acm.tju.edu.cn/toj/showp.php?pid=1139
解题思路:
简单的最长公共子序列问题,只不过过程中比较的是两个字符串,故使用二维字符数组保存输入文本。
输入 x[1...m][], y[1...n][] ,c[i,j]代表两个文本的LCS的长度,递归方程如下:
c[0,j] = c[i,0] = 0;
c[i,j] = c[i-1,j-1] + 1 if x[i]==y[j]
c[i,j] = max(c[i-1,j], c[i,j-1]) if x[i]!=y[j]
使用 b[i,j] 表示三种情况(=1,=2,=3),方便以后输出LCS:
if b[i,j] == 1,表示 x[i] == y[j], 可以输出;
if b[i,j] == 2,表示 c[i-1,j] > c[i,j-1], i--即可;
if b[i,j] == 3,表示 c[i,j-1] > c[i-1,j], j--即可;
源代码:
/*
OJ: TOJ
ID: 3013216109
TASK: 1139.Compromise
LANG: C++
NOTE: LCS(DP)
*/
#include <iostream>
#include <cstring>
using namespace std; int main()
{
char x[][],y[][],ans[][];
int c[][],b[][];
int i,j,k,m,n;
while(cin >> x[]) {
for(i=;;i++) {
cin >> x[i];
if(x[i][]=='#')break;
}
for(j=;;j++) {
cin >> y[j];
if(y[j][]=='#')break;
}
m=i-; n=j-;
for(i=;i<=m;i++)
c[i][]=;
for(i=;i<=n;i++)
c[][i]=;
for(i=;i<=m;i++) {
for(j=;j<=n;j++) {
if(!strcmp(x[i],y[j])) {
c[i][j]=c[i-][j-]+;
b[i][j]=;
}
else if(c[i-][j]>=c[i][j-]) {
c[i][j]=c[i-][j];
b[i][j]=;
}
else {
c[i][j]=c[i][j-];
b[i][j]=;
}
}
}
i=m;j=n;
k=c[m][n]-;
while(i>&&j>&&k>=) {
if(b[i][j]==) {
strcpy(ans[k],x[i]);
i--;j--;k--;
}
else if(b[i][j]==) i--;
else if(b[i][j]==) j--;
else break;
}
for(i=;i<c[m][n]-;i++)
cout << ans[i] <<" ";
cout <<ans[c[m][n]-]<<endl;
}
return ;
}
TOJ 1139.Compromise的更多相关文章
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- a compromise between lock overhead and data safety
High Performance My SQL THIRD EDITION A locking strategy is a compromise between lock overhead and ...
- URAL 1139 City Blocks(数论)
The blocks in the city of Fishburg are of square form. N avenues running south to north and Mstreets ...
- POJ2250:Compromise(LCS)
Description In a few months the European Currency Union will become a reality. However, to join the ...
- TOJ 1702.A Knight's Journey
2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...
- [Swust OJ 1139]--Coin-row problem
题目链接: http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...
- POJ 2250 Compromise(LCS)
POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...
- UVA 531 - Compromise(dp + LCS打印路径)
Compromise In a few months the European Currency Union will become a reality. However, to join th ...
- 优先队列运用 TOJ 4123 Job Scheduling
链接:http://acm.tju.edu.cn/toj/showp4123.html 4123. Job Scheduling Time Limit: 1.0 Seconds Memory ...
随机推荐
- D. DZY Loves Modification
D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- mvp框架
本文在于巩固基础 mvp框架的概念: MVP是MVC模式的另一个变种,MVP即可以应用到WEB项目中, 也可以应用到Winform项目中,它的方便的测试机制为大型复杂的企业级应用带来了福音,MVP模式 ...
- html5滑动手势
<div id="divMove" style="height: 100px;"></div> <div id="sli ...
- android监听键盘
android中的带有输入功能的页面布局经常被弹出的键盘遮挡,一种处理方法是监听键盘的弹出,设置布局的padding或隐藏某些占位控件,使得输入框不被键盘遮挡.一种常用的方法是当Activity设置为 ...
- Java SE基础部分——常用类库之NumberFormat(数字格式化)
数字格式化常用方法:DecimalFormat和NuberFormat. //2016060524 数字格式化学习 //数字格式化 两种方法 一种直接使用NumberFormat,另一种Decimal ...
- Python核心编程读笔 11:模块
第12章 模块 1.基本概念 模块的文件名就是模块名字.py 每个模块都定义了自己唯一的名称空间 模块的搜索路径:会被保存在 sys 模块的 sys.path 变量里 >>>sys. ...
- EC读书笔记系列之10:条款16、17
条款18 让接口容易被正确使用,不易被误用 记住: ★“促进正确使用”的办法包括接口的一致性,以及与内置类型的行为兼容 ★“阻止误用”的办法包括建立新类型.限制类型上的操作,束缚对象值,以及消除客户的 ...
- WTL CEdit关联绑定ID,滚动到最新的一行
绑定控件 HWND logEdit = ::GetDlgItem(this->m_hWnd, IDC_EDIT_LOG); m_outputlogEdit.Attach(logEdit); 滚动 ...
- plupload文件上传插件
一 资源文档 二 基本使用 三 可能遇到的问题 一 资源文档 Git仓库地址:https://github.com/moxiecode/plupload 一个中文速查:http://www.cnblo ...
- [LeetCode]题解(python):153-Find Minimum in Rotated Sorted Array
题目来源: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ 题意分析: 在一个不重复的翻转的数组里面找到最小那个 ...