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. Tkinter类之窗口部件类

    Tkinter类之窗口部件类 Tkinter支持15个核心的窗口部件,这个15个核心窗口部件类列表如下:窗口部件及说明:Button:一个简单的按钮,用来执行一个命令或别的操作.Canvas:组织图形 ...

  2. javascript第七课js函数

    function add() { } 上面就是js中的方法,js中的方法与c#中的方法不同的是不需要写返回值类型 function  add(num1,num2) { return num1+num2 ...

  3. C# DataTable转实体 通用方法【转】

    public static T GetEntity<T>(DataTable table) where T : new()    {        T entity = new T();  ...

  4. C#中各种计时器

    1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch) Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 S ...

  5. 使用EXTEND方式来分段处理大表的搬数据

    创建一个表: 记录rowid的分区段并作为处理的日志表: DROP TABLE DEAL_TABLE_EXTENT; CREATE TABLE DEAL_TABLE_EXTENT(seq        ...

  6. android笔试题

    1.请谈一下Android系统的架构. 答:Android系统采用了分层架构,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层.   2.谈谈android大众常用的五种布 ...

  7. Demo 示例控制输入光标位置

    <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <meta name="aut ...

  8. Swift 函数和类

    函数: func sayHello(personName:String,z:Int)->{ return "hello"+personName+z } print(sayHe ...

  9. php面向对象编程学习之高级特性

    前几天写了一篇关于php面向对象基础知识的博客,这两天看了php面向对象的高级特性,写出来记录一下吧,方便以后拿出来复习. 面向对象除了最基本的定义类之外,最主要就是因为面向的一些高级特性,运用这些高 ...

  10. docker 私有仓库镜像的存储位置

    docker 私有仓库的镜像 是存储在5739360d1030 registry "docker-registry" 3 days ago Up 28 hours 0.0.0.0: ...