一、题目

  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. python sort() sorted()的区别

    sorted不保存 sort保存:

  2. Petrozavodsk Winter Training Camp 2017G(栈)题解

    题意: \(M_i\)为一个\(m*m\)矩阵,已知 \[\begin{aligned} &M_0=A\\ &M_i=(\prod_{j=c_i}^{i+1}M_j)B \end{al ...

  3. 一分钟搞懂JavaScript中的JSON对象

    JSON(JavaScript Object Notation)是表示值和对象的通用格式. JavaScript 提供了如下方法: JSON.stringify 将对象转换为 JSON. JSON.p ...

  4. MobX All In One

    MobX All In One Simple, scalable state management. https://mobx.js.org/README.html https://github.co ...

  5. CSS & SASS & SCSS & less

    CSS & SASS & SCSS & less less vs scss https://github.com/vecerek/less2sass/wiki/Less-vs. ...

  6. scrollTo & js auto scroll & scrollX & scrollY

    scrollTo & js auto scroll & scrollX & scrollY scrollX & scrollY 获取 scroll top height ...

  7. nasm astrchr函数 x86

    xxx.asm: %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export as ...

  8. ng 设置动态的document title

    使用Title服务 相关文章 配置路由, 添加data.title参数 import { NgModule } from '@angular/core'; import { RouterModule, ...

  9. DeFi热下的冷思考 NGK以更深层次的方式参与DeFi建设

    独具慧眼,深度挖掘DeFi潜力项目 作为早期DeFi的探索者和推动者,NGK在此轮热潮席卷市场前就已经开始构建自己的DeFi生态,独具慧眼的NGK上线了Baccarat流动性挖矿项目,完成了首个由平台 ...

  10. 数据归一化 scikit-learn中的Scaler

    1 import numpy as np 2 from sklearn import datasets 3 4 # 获取数据 5 iris = datasets.load_iris() 6 X = i ...