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. ASP.NET Web API Authorization using Tokens

    Planning real world REST API http://blog.developers.ba/post/2012/03/03/ASPNET-Web-API-Authorization- ...

  2. Java学习笔记-File类的基本方法

    要渐渐养成写博客的习惯-----> 前段时间看Mars的java中的I/O流没怎么懂,发现I/O流好难啊.今天重新看一遍其他教学,还有书籍,做些笔记,记录下每天的学习生活. File类的一些方法 ...

  3. C#的Split用法

    1.用字符串分隔: using System.Text.RegularExpressions;string str="aaajsbbbjsccc";string[] sArray= ...

  4. JavaScript中的计时器原理

    理解John Resig 在 How JavaScript Timers Work. 原理分析 timer(setInterval,setTimeout)有一个很重要的概念,时间延迟的长短是不稳定的. ...

  5. 执行CMD代码

    /// <summary> /// 发送CMD命令(执行命令行) /// </summary> public static void SendCMD(string pwd) { ...

  6. Web 应用性能提升 10 倍的 10 个建议

    转载自http://blog.jobbole.com/94962/ 提升 Web 应用的性能变得越来越重要.线上经济活动的份额持续增长,当前发达世界中 5 % 的经济发生在互联网上(查看下面资源的统计 ...

  7. leetcode Combination Sum python

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  8. PHP多线程的实现(PHP多线程类)

    通过WEB服务器来实现PHP多线程功能. 当然,对多线程有深入理解的人都知道通过WEB服务器实现的多线程只能模仿多线程的一些效果,并不是真正意义上的多线程. 但不管怎么样,它还是能满足我们的一些需要的 ...

  9. php 封装Mysql数据库操作类

    花了点时间写了个基于php5.3的Mysql类 $mysql = new Mysql('host','user','pass','db') bool Mysql::insert("表&quo ...

  10. php实现两分法查找

    两分法查找的前提:顺序方式存储,而且必须是排好序 直接上代码: function search($array, $target, $low = 0, $high = 0){ $len = count( ...