POJ_1458 Common Subsequence 【LCS】
一、题目
二、分析
比较基础的求最长升序子序列。
$DP[i][j]$表示的是字符串$S1[1...i]$与$S2[1...j]$的最长公共子序列长度。
状态转移:$$if s1[i] == s2[j] DP[i][j] = DP[i-1][j-1] + 1$$ $$if s1[i] != s2[j] DP[i][j] = max(DP[i-1][j], DP[i][j-1]$$
相等时好理解,不相等的时候就是考虑两个字符串分别加上这个字符后,最长的公共子序列长度。
三、AC代码
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <algorithm>
5 #include <vector>
6 #include <cmath>
7
8 using namespace std;
9 #define ll long long
10 #define Min(a,b) ((a)>(b)?(b):(a))
11 #define Max(a,b) ((a)>(b)?(a):(b))
12 const int MAXN = 1e3;
13 char s[MAXN+13], s2[MAXN+13];
14 int DP[MAXN+13][MAXN+13];
15
16 int main()
17 {
18 while(scanf("%s%s", s, s2) != EOF) {
19 memset(DP, 0, sizeof(DP));
20 int L1 = strlen(s), L2 = strlen(s2);
21 for(int i = 1; i <= L1; i++) {
22 for(int j = 1; j <= L2; j++) {
23 if(s[i-1] == s2[j-1]) {
24 DP[i][j] = DP[i-1][j-1] + 1;
25 }
26 else {
27 DP[i][j] = Max(DP[i-1][j], DP[i][j-1]);
28 }
29 }
30 }
31 printf("%d\n", DP[L1][L2]);
32 }
33 return 0;
34 }
POJ_1458 Common Subsequence 【LCS】的更多相关文章
- hdoj 1159 Common Subsequence【LCS】【DP】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- poj 1458 Common Subsequence【LCS】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: 17 ...
- HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1159 Common Subsequence 【LCS 基础入门】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1159 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】
POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- POJ1458 Common Subsequence 【最长公共子序列】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37614 Accepted: 15 ...
- HDU 1159 Common Subsequence【dp+最长公共子序列】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- keepalived.conf说明
keepalived.conf说明 发表于 2017-06-04 | 分类于 运维相关 , Keepalived | | 阅读次数 348 | 字数统计 1,889 | 阅读时长预计 8 本文主要介绍 ...
- 信号量解决理发师问题(barber)
问题描述及思路 代码 一些细节见注释 这里ret应该用int..忘了改了. 运行结果 因为座位数和到来最大间隔的原因,没有出现全部椅子被占用的情况
- Cortex-M系列内核 启动文件分析
最近终于闲了下来了准备好好学习下Cortex-M3/M4系列处理器的架构,经过各种资料的折磨也没法对它的整个工作过程能有个完整的认知,最后看到一片博客打算从程序的运行过程开始探究,所以首先就找到了启动 ...
- Seven xxx in Seven Weeks ebooks | 七周七 xxx 系列图书 电子书| share 分享 | free of charge 免费!
Seven xxx in Seven Weeks ebooks | 七周七 xxx 系列图书 电子书| share 分享 | free of charge 免费! Seven Languag ...
- Ajax & JSONP 原理
Ajax & JSONP 原理 AJAX不是JavaScript的规范,它只是一个哥们"发明"的缩写:Asynchronous JavaScript and XML,意思就 ...
- How to build a sortable table in native js?
How to build a sortable table in native/vanilla js? H5 DnD https://developer.mozilla.org/zh-CN/docs/ ...
- Github Actions All In One
Github Actions All In One https://github.com/features/actions https://github.com/marketplace?type=ac ...
- Scratch & Flappy Turtle & Flappy Bird & Game
Scratch & Flappy Turtle & Flappy Bird & Game Flappy Turtle Game https://scratch.mit.edu/ ...
- css3 units & 1 turn
css3 units & 1 turn One full circle is 1turn, 180deg === 0.5turn degrees, gradians, radians, tur ...
- 灰度发布 & A/B 测试
灰度发布 & A/B 测试 http://www.woshipm.com/pmd/573429.html 8 https://testerhome.com/topics/15746 scree ...