最长公共子序列,经典问题。算是我的DP开场题吧。

dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度。

状态转移:

dp[i][j] = 0                                       (i == 0 || j == 0)

dp[i][j] = dp[i-1][j-1] + 1                   (s1[i] == s2[j]) (此字符相等,说明此时最长公共子序列的长度等于他们之前的LCS(简称)长度加上这个相等的字符(长度为1))

dp[i][j] = max(dp[i-1][j],dp[i][j-1])      (s1[i] != s2[j]) (此字符不相等,说明此时LCS为i减小一位再求LCS的长度以及j减小一位再求LCS的长度的最大值。即为LCS)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
#define N 1007 int dp[N][N],n;
int main()
{
int m,n,i,j;
string s1,s2;
while(getline(cin,s1) && getline(cin,s2))
{
m = s1.length();
n = s2.length();
s1 = " " + s1;
s2 = " " + s2;
for(i=;i<=m;i++)
dp[i][] = ;
for(j=;j<=n;j++)
dp[][j] = ;
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
if(s1[i] == s2[j])
dp[i][j] = dp[i-][j-] + ;
else
dp[i][j] = max(dp[i-][j],dp[i][j-]);
}
}
printf("%d\n",dp[m][n]);
}
return ;
}

(注意这里用string的话,不能cin>>s1>>s2,虽然我也不知道哪里不对。还是用getline保险一点。如果是字符数组的话,就用gets或者scanf都可以。)

UVA 10405 Longest Common Subsequence --经典DP的更多相关文章

  1. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  2. UVA 10405 Longest Common Subsequence

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...

  3. [UVa OJ] Longest Common Subsequence

    This is the classic LCS problem. Since it only requires you to print the maximum length, the code ca ...

  4. 2017-5-14 湘潭市赛 Longest Common Subsequence 想法题

    Longest Common Subsequence Accepted : Submit : Time Limit : MS Memory Limit : KB Longest Common Subs ...

  5. Longest Common Subsequence (DP)

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  6. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  7. [Algorithms] Longest Common Subsequence

    The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...

  8. leetcode1143 Longest Common Subsequence

    """ Given two strings text1 and text2, return the length of their longest common subs ...

  9. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

随机推荐

  1. Verilog学习笔记基本语法篇(十)········ 常用系统函数

    $display 和 $write 任务 格式: $display (p1,p2,...,pn); $write (p1,p2,..,pn); 这两个函数和系统的任务作用是用来输出信息,即将参数p2到 ...

  2. Linux Shell系列教程之(十五) Shell函数简介

    本文是Linux Shell系列教程的第(十五)篇,更多Linux Shell教程请看:Linux Shell系列教程 函数可以将一个复杂功能划分成若干模块,从而使程序结构更加清晰,代码重复利用率更高 ...

  3. AutoMapper映射ExpressionTree

    问题描述 项目中使用AutoMapper进行VO&DTO&Entity的互相映射,但是默认Map方法不支持Expression的转换.如 Expression<Func<E ...

  4. 项目、SVN clean的一些事

    1.如果你发现你的文件修改了.Tomcat也重新了,但访问的还是旧的文件,这个时候你需要clean下你的项目. Clean will discard all build problems and bu ...

  5. 被混淆的C#类库的反编译

    今天看公司以前的代码,用的是.NRT Reactor v4.4.7.5进行的混淆,直接使用.NET Reflector v8.5.0.179 是无法查看的,提示:Invalid number of d ...

  6. 配置windows失败,还原更新,请勿关机

    同事叫我帮忙弄一下电脑,开机,出现"配置Windows Update失败,还原更改,请勿关闭计算机",我从来不更新Windows Update,更新都为成功,第一次遇到失败了,不知 ...

  7. java随机生成指定的位数

    /** * 获取count个随机数 * @param count 随机数个数 * @return */ public static String game(int count){ StringBuff ...

  8. 网络开始---多线程---GCD-01-基本使用(掌握)(六)

    /** GCD两个核心概念:任何和队列 任务:执行什么操作 队列:用来存放任务 使用就2个步骤 1.定制任务 2.将任务添加到队列中 任务的取出队列原则:FIFO原则: 先进先出,后进后出 */ #i ...

  9. 在iOS 8中使用UIAlertController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...

  10. MySQL学习基础 之 起航篇

    MySQL 学习来自慕课网<与MySQL的零距离接触> MySQL是一个开源的关系型数据库管理系统 MySQL分为社区版和企业版 MySQL登录和退出相关的命令 参数 描述 -D,--da ...