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的更多相关文章

  1. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  2. a compromise between lock overhead and data safety

    High Performance My SQL  THIRD EDITION A locking strategy is a compromise between lock overhead and ...

  3. URAL 1139 City Blocks(数论)

    The blocks in the city of Fishburg are of square form. N avenues running south to north and Mstreets ...

  4. POJ2250:Compromise(LCS)

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  5. TOJ 1702.A Knight's Journey

    2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...

  6. [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 ...

  7. POJ 2250 Compromise(LCS)

    POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...

  8. UVA 531 - Compromise(dp + LCS打印路径)

      Compromise  In a few months the European Currency Union will become a reality. However, to join th ...

  9. 优先队列运用 TOJ 4123 Job Scheduling

    链接:http://acm.tju.edu.cn/toj/showp4123.html 4123.   Job Scheduling Time Limit: 1.0 Seconds   Memory ...

随机推荐

  1. python 访问器@property的使用方法

    @property 可以将python定义的函数"当做"属性访问,从而提供更加友好访问方式,但是有时候setter/getter也是需要的 假设定义了一个类Cls,该类必须继承自o ...

  2. Minimum Transport Cost(floyd+二维数组记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  3. SQL SERVER 中 实现主表1行记录,子表多行记录 整合成一条虚拟列

    表中有这样的记录,简单的主子表,现要想通过left join 语句把两表关联起来 select * from tbl_diary_reback a left join tbl_diary_reback ...

  4. html系列教程--header head iframe img

    <head> 标签 用于定义文档的头部,它是所有头部元素的容器.<head> 中的元素可以引用脚本.指示浏览器在哪里找到样式表.提供元信息 head的描述信息包括<bas ...

  5. Java 基本日期类使用——格式化(二)

    Java日期格式化主要有以下几种方式:java.text.DateFormat以及其子类java.text.SimpleDateFormat; DateFormat 是日期/时间格式化子类的抽象类,它 ...

  6. iOS修改截取图片不规范问题

    +(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{ UIImage ...

  7. C#获得命令提示符输出

    原文:http://blog.csdn.net/abrahu/article/details/6611504 C#获得命令提示符输出 分类: c#应用程序2011-07-16 23:34 600人阅读 ...

  8. 监听enter事件

    document.onkeydown=keyDownSearch; function keyDownSearch(e) { // 兼容FF和IE和Opera var theEvent = e || w ...

  9. Cookies与保持登录(新浪微博的简单模拟登录)

    Cookies与保持登录(新浪微博的简单登录) .note-content {font-family: "Helvetica Neue",Arial,"Hiragino ...

  10. Hibernate之总结

    以前做.net,最近做java项目,负责服务端的开发,直接用的jdbc,线程安全问题.缓存同步问题以及连接池什么的,都是手动写,不但麻烦而且容易出错.项目结束,赶快抽时间学了下hibernate,每天 ...