一、题目

  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. 数据库之postgreSQL入门操作指南

    一.增 二.删 三.改 四.查 五.SQL操作表 1.增加列 ALTER TABLE table_name ADD column_name datatype; 2.删除一列 ALTER TABLE t ...

  2. C++ 变量声明数组

    int len; cin>>len; int *p=new int[len]; delete[] p; 不能写作 int p[]=new int[len]; 因为new是开辟了内存空间后返 ...

  3. vector最最最基础用法(非原创)

    在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. 1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<in ...

  4. jQuery 使用注意事项 与 小技巧(tips)

    jQuery 使用注意事项 与 小技巧(tips) 1 $( document ).ready() https://learn.jquery.com/using-jquery-core/documen ...

  5. Web Components All In One

    Web Components All In One Web Components https://www.webcomponents.org/ HTML Template Custom Element ...

  6. Dart Generic All In One

    Dart Generic All In One Dart 泛型 https://dart.dev/guides/language/language-tour#generics /** * * @aut ...

  7. codesign wants to access key 密码是什么

    codesign wants to access key 密码是什么 真正的是开机密码,不是 apple id 密码 https://developer.apple.com/forums/thread ...

  8. qrcode & console.log

    qrcode & console.log image https://fs-api.lightyy.com/service/utils/qrcode?url=http://169.254.13 ...

  9. js 动态构建style

    使用创建style的方式 btn.addEventListener("click", async () => { const ns = document.createElem ...

  10. Flutter: Draggable和DragTarget 可拖动小部件

    API class _MyHomeState extends State<MyHome> { List<Map<String, String>> _data1 = ...