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 ...
随机推荐
- Python生成器、迭代器、可迭代对象
把一个列表[]改成()就创建了一个生成器:generator,generator保存的是算法. 可以用于for循环的数据类型:1.集合类型:list tuple dict set str2.gener ...
- [程序人生]前途无"亮‘’的大学
转眼之间就到大四了,今天晚上很迷茫,很纠结,想了好多,好多,真的,长大之后,自从第一次失恋之后,第一次会想到这么的多. 嗯,先自我介绍哈吧,我是从云南的大山里走出来的孩子,什么样的大山,就是到 ...
- [WP8] 使用ApplicationMenu与使用者互动
[WP8] 使用ApplicationMenu与使用者互动 范例下载 范例程序代码:点此下载 功能说明 使用过Lumia系列手机的开发人员,对于内建的相机功能相信都很熟悉.在Lumia内建的相机功能中 ...
- IOS网络编请求响应之URL结构
资料均来自互联网,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. 人魔七七:http://www.cnblogs.com/qiqibo/ 对于我们IOS开发者来说 ...
- GCD中使用dispatch_after函数延迟处理任务
在实际的开发中,经常会遇到想要在指定的时间间隔后执行某个处理 <一>在GCD中提供了dispatch_after函数来完成这一操作 dispatch_after(dispatch_time ...
- 如何避免Activity 被杀死
我们都知道,在android系统中,内存不足的时候,系统是可以杀死任何暂停.停止或者销毁的Activity.这就意味着基本上没有在前台的Activity都会面临被关闭的可能. Android系统之所以 ...
- 我体验过的可以用的XCode插件
XCode版本:7.0.1 其实插件的使用,如果能得到直接的xclugin后缀文件,是可以直接放置在隐藏文件夹中: /Users/HeYang/Library/Application Support/ ...
- [Tomcat]如何在同一台机部署多个tomcat服务
背景:往往不知情的同学在同一台机器上部署多个tomcat会发现第二个tomcat启动会报错.而有些同学会想到可能是端口重复,然而,在server.xml改了端口还是发现不行.其实要想实现同一台机器部署 ...
- 5+ App开发入门指南
HTML5 Plus应用概述 HTML5 Plus移动App,简称5+App,是一种基于HTML.JS.CSS编写的运行于手机端的App,这种App可以通过扩展的JS API任意调用手机的原生能力,实 ...
- mmap 与 read/write
mmap与read/write两条路线对文件的访问比较 我们知道无论是通过mmap或read/write访问文件在内核中都必须经过缓存, 当需要从文件读写内容时,都经过内存拷贝的方式与内核中的缓存进行 ...