一、题目

  Common Subsequence

二、分析

  比较基础的求最长升序子序列。

  $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】的更多相关文章

  1. hdoj 1159 Common Subsequence【LCS】【DP】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. poj 1458 Common Subsequence【LCS】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 17 ...

  3. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  4. hdu 1159 Common Subsequence 【LCS 基础入门】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1159 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  6. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  7. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  8. POJ1458 Common Subsequence 【最长公共子序列】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37614   Accepted: 15 ...

  9. HDU 1159 Common Subsequence【dp+最长公共子序列】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 网络安全-企业环境渗透2-wordpress任意文件读&&FFmpeg任意文件读

    参考 http://prontosil.club/posts/c08799e1/ 一. 实验名称 企业环境渗透2 二. 实验目的 [实验描述] 操作机的操作系统是kali 进入系统后默认是命令行界面 ...

  2. window.onresize使用实例

    <!DOCTYPE html> <html> <head> <title>请调整浏览器窗口</title> <meta charset ...

  3. ysoserial Commons Collections1反序列化研究

    Apache Commons Collections1反序列化研究 环境准备 Apache Commons Collections 3.1版本 IDEA 需要一些java基础,反射.类对象.Class ...

  4. Bash on Ubuntu on Windows ( Windows Subsystem for Linux)

    1 #  Bash on ubuntu on Windows http://www.cnblogs.com/anonymous-ufo/p/6143480.html 1 1 如何启用Bash on u ...

  5. LeetCode 算法面试题汇总

    LeetCode 算法面试题汇总 算法面试题 https://leetcode-cn.com/problemset/algorithms/ https://leetcode-cn.com/proble ...

  6. SwiftUI error All In One

    SwiftUI error All In One Instance member xxx cannot be used on type yyy Instance member 'game' canno ...

  7. 找出 int 数组的平衡点 & 二叉树 / 平衡二叉树 / 满二叉树 / 完全二叉树 / 二叉查找树

    找出 int 数组的平衡点 左右两边和相等, 若存在返回平衡点的值(可能由多个); 若不存在返回 -1; ``java int [] arr = {2,3,4,2,4}; ```js const ar ...

  8. React Native Apps

    React Native Apps https://github.com/ReactNativeNews/React-Native-Apps github app https://github.com ...

  9. Array in Depth

    Array in Depth Array.concat() & Array.push() https://developer.mozilla.org/en-US/docs/Web/JavaSc ...

  10. element-ui的树型结构图,半选状态数据给后台后,返回数据带有半选父节点的剔除展示

    // html <h2 class="text-gray">功能权限</h2><el-tree :data="permissionList& ...