UVA 10405 Longest Common Subsequence --经典DP
最长公共子序列,经典问题。算是我的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的更多相关文章
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
- UVA 10405 Longest Common Subsequence
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...
- [UVa OJ] Longest Common Subsequence
This is the classic LCS problem. Since it only requires you to print the maximum length, the code ca ...
- 2017-5-14 湘潭市赛 Longest Common Subsequence 想法题
Longest Common Subsequence Accepted : Submit : Time Limit : MS Memory Limit : KB Longest Common Subs ...
- Longest Common Subsequence (DP)
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- [Algorithms] Longest Common Subsequence
The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...
- leetcode1143 Longest Common Subsequence
""" Given two strings text1 and text2, return the length of their longest common subs ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
随机推荐
- Runtime -----那些被忽略的技能
有人说现在的程序员都被惯坏了,尤其使用一些面向对象的语言开发的时候,只是简单的调用一些系统封装好的接口或者是调用一些“便利的”第三方,对于一个程序的真正实现有了解吗???又有多少了解呢 ...
- [js开源组件开发]js手机联动选择日期 开源git
js手机联动选择日期 这里在前面的<js手机联动选择地区>的基础上,改造数据源之后形成的一个日期的选择器,当然你可以使用之前的PC上模式的<日期控件>,它同时也支持手机端,ht ...
- ArcGIS使用Python脚本工具
在Pyhton写的一些代码,用户交互不方便,用户体验比较差,不方便重用.在ArcGIS中可以将用写的Python代码导入到ToolBox中,这样用起来就比较方便了.这里用按要素裁剪栅格的Python来 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q32-Q34)
Question 32You create a custom Web Part.You need to ensure that a custom property is visible in Edit ...
- 我在用的mac软件(2)-终端环境之zsh和z(*nix都适用)
继续上篇介绍我的终端环境.这篇介绍zsh和z,其实这不局限于os x,在所有的*nix系统中都是可用的. zsh zsh作为bash的替代品,自然很多人要问:why zsh? 在Zsh Worksho ...
- Android 带清除功能的输入框控件EditText
1.效果图 2.源码下载 http://download.csdn.net/detail/yanzi2015/8864603 3.相关博客 http://www.cnblogs.com/to ...
- MacOs终端忽略大小写
使用MacOs的终端时,唯一让人感觉不爽的就是Tab补全是区分大小的,所以查了资料就把这个问题搞定了.在用户目录下创建 .inputrc 文件,内容为以下三行代码,保存后重启终端再次输入文件名Tab补 ...
- jQuery点击事件绑定onClick和on()
一.静态绑定 (1)onclick方法 jsp代码 <button href="javascript:;" class="weui_btn weui_btn_min ...
- iOS之 PJSIP静态库编译(三)
dada哪个所有静态库编译完成后还是不能运行那个demo,提示你找不到arm**.a 你lipo后要记得吧合并成.a 名字更改成你最后编译版本生成的.a名字....... 或者吧所有库add到你的工 ...
- iOS之多控制器管理--项目中的常见文件
项目中的常见文件 内容大纲: 1.LaunchScreen 2.info.plist文件 3.pch文件 1.LaunchScreen xcode5和xcode6区别 1.xcode6没有Framew ...