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 ...
随机推荐
- Maven的安装使用以及 Maven+Spring hello world example
关于Maven Maven是一个用于项目构建的工具,通过它便捷的管理项目的生命周期.即项目的jar包依赖,开发,测试,发布打包. 做过.NET的人应该会联想到Nuget,是的Maven其实就是java ...
- weblogic 12c下jxls导出excel报错Could not initialize class org.apache.poi.xssf.usermodel.XSSFVMLDrawing
周一,开发反馈weblogic 12c下jxls导出excel报错,公司环境和UAT环境均报错,看日志如下: 2016-06-08 09:16:55,825 ERROR org.jxls.util.T ...
- [转]MVC、MVP、MVVM
界面之下:还原真实的 MVC.MVP.MVVM 模式 [日期:2015-10-28] 来源:github.com/livoras 作者:戴嘉华 [字体:大 中 小] 前言 做客户端开发.前端开发 ...
- swift学习笔记之-继承
//继承 import UIKit /* 继承(Inheritance): 1.一个类可以继承(inherit)另一个类的方法(methods).属性(properties)和其它特性.当一个类继承其 ...
- arcgis union 0x80040218
我利用ArcGis中的union工具将两张图层叠加时,系统总是提示失败,这是什么原因?希望高手能够解决这个问题.图片是系统提示,表示看不懂哪里出错了? 你必须使用数据管理-要素-修复几何,源数据存在不 ...
- Snort - manual 笔记(二)
1.5 Packet Acquisition Snort 2.9 引入 DAQ 代替直接调用 libpcap . 有两种网卡特性会影响 Snort : "Large Receive Offl ...
- 【转载】菜鸟Ubuntu下安装Android Studio
原文:http://forum.android-studio.org/forum.php?mod=viewthread&tid=236&extra=page%3D1%26filter% ...
- 明明已经执行Log.i,偏偏打不出日志
Android内打日志用的当然是Log.i(tag,string),调试时的日志输出可以很快的反映一些问题,方便我们跟进. 但是如果连日志都打不出来了怎么办呢,我今天就遇到了比较坑的问题.项目里别的日 ...
- OpenGL ES学习笔记(三)——纹理
首先申明下,本文为笔者学习<OpenGL ES应用开发实践指南(Android卷)>的笔记,涉及的代码均出自原书,如有需要,请到原书指定源码地址下载. <OpenGL ES学习笔记( ...
- 使用CocoaPods管理第三方开源类库
iOS开发中经常会用到许多第三方开源类库,比如AFNetworking.FMDB.JSONKit等等,使用CocoaPods这个工具就能很方便得对工程中用到的类库进行管理,包括自动下载配置以及更新. ...