1. 程式人生 > >POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】

POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;


const int maxn = 500+50;
int dp[maxn][maxn];
int a[maxn],b[maxn];
int m,n;


/****
求序列 A 長度為 N 和序列 B 長度為 M 的 LCS
序列下標從 1 開始
*/
int LCS()
{
    for(int i = 1; i <= n; i++)
    {
        int tmp = 0; //記錄在i確定,且a[i]>b[j]的時候dp[i,j]的最大值
        for(int j = 1; j <= m; j++)
        {
            dp[i][j] = dp[i-1][j];
            if(a[i] > b[j])
            {
                tmp = dp[i-1][j];
            }
            else if(a[i] == b[j])
                dp[i][j] = tmp+1;
        }
    }
//for(int i = 1; i <= m; i++) printf("%d ", dp[n][i]); printf("\n");


    int ans = 0;
    for(int i = 1; i <= m; i++)
        ans = max(ans, dp[n][i]);
    return ans;


}


int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));


        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        scanf("%d", &m);
        for(int j = 1; j <= m; j++)
            scanf("%d", &b[j]);


        printf("%d\n",LCS());
        if(T != 0) printf("\n");
    }
}


相關推薦

POJ 1423 Greatest Common Increasing SubsequenceLCIS

#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; const int maxn = 500+50; int dp[

1014 - 最長公共上升子序列 - Greatest Common Increasing Subsequence(HDU 1423

傳送門   分析 好優秀的一道題啊…… 我學過最長公共子序列,也會最長上升子序列 但mix在一起,我就糊塗了 其實也還好 我們相當於在最長公共子序列的基礎上多一個條件:要求數值遞增 還記得最長公共子序列是怎麼求的嗎? 定義:F[i,j]表示處

HDU1423:Greatest Common Increasing Subsequence

Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis

AOJ_ALDS1_10_C Longest Common SubsequenceLCS+DP

Longest Common Subsequence Aizu - ALDS1_10_C For given two sequences X and Y, a sequence Z is a common subsequence of X and Y if Z is a subsequence of bot

UVA10405 Longest Common SubsequenceLCS+DP

Given two sequences of characters, print the length of the longest common subsequence of both sequences.     Sequence 1:   

POJ-3087 Shuffle'm Up 暴力+STL

題目傳送門 題目:t組資料,給出長度為n的初始字串s1,s2,和長度為2n的目標字串p,按題目要求操作,問s1,s2達到字串p的最小運算元,若達不到則輸出-1;輸出時首先輸出資料組數。 題解:直接用字串模擬,用map記錄該狀態是否被訪問過,如果被訪問過則說明出現迴圈,到達不了目標狀態。

HDU4632Palindrome subsequence區間DP

題目大意: 題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4632 給出 t t

3530 Subsequence單調佇列

Time limit 1000 ms Memory limit 32768 kB There is a sequence of integers. Your task is to find the longest subsequence that satisfi

poj 3046 多重集組合數水一下

題目大意:有t個螞蟻種群,共a只螞蟻,從這些種群中挑出n只螞蟻,方法共有x[n]種,計算x[s]+x[s+1]+…+x[b]。 這是一道多重集組合數題目。用dp[i][j]表示前i中種群中挑出j只螞蟻的方案數。依據《挑戰程式競賽(第二版)》 P68-P69公式

POJ 1066 Treasure Hunt [線段相交]計算幾何

Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6260 Accepted: 2598 Description Archeolog

hdu3308 LCIS線段樹pushup最長連續上升序列

【連結】 http://acm.hdu.edu.cn/showproblem.php?pid=3308 【題意】 給你n個數,m個操作。 操作有兩種: 1.U x y 將陣列第x位變為y   2. Q x y 問陣列第x位到第y位連續最長子序列的長度

的並查集POJ 1611 The Suspects

space lose %d cst one accep poj find can http://poj.org/problem?id=1611 【Accepted】 1 #include<iostream> 2 #include<cstdio>

筆記最長遞增子序列 Longest increasing subsequence(LIS)

http range element -m 元素 筆記 pro 最長公共子序列 .org 介紹和解法,參見wikipedia https://en.wikipedia.org/wiki/Longest_increasing_subsequence 筆記: 在按下標順序遍歷序

POJ 2533 Longest Ordered SubsequenceLIS)

rate ces iss plm total desc problem limit find 傳送門: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS M

POJ 1470 Closest Common Ancestors (模板題)(Tarjan離線)LCA

clear pac push 公共祖先 back family ble lan tarjan <題目鏈接> 題目大意:給你一棵樹,然後進行q次詢問,然後要你統計這q次詢問中指定的兩個節點最近公共祖先出現的次數。 解題分析:LCA模板題,下面用的是離線Tarjan

HDU1159 Common Subsequence最長公共子序列

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4952

POJ 1330 Nearest Common Ancestors (模板題) (LCA)倍增

<題目連結> 題目大意: 給出一棵樹,問任意兩個點的最近公共祖先的編號。 解題分析:LCA模板題,下面用的是線上倍增演算法求解。 1 #include <cstdio> 2 #include <cstring> 3 #include <algori

python3leetcode 674. Longest Continuous Increasing Subsequence(easy)

674. Longest Continuous Increasing Subsequence(easy) Given an unsorted array of integers, find the length of longest continuous incr

HDU 1159.Common Subsequence動態規劃DP3月8

Common Subsequence Problem Description A subsequence of a given sequence is the given sequence with some elements (possible none) lef

LeetCode & 劍指offer刷題動態規劃與貪婪法題10:Longest Increasing Subsequence

rst 貪婪 float tor leet rep 大於等於 lower ola 【LeetCode & 劍指offer 刷題筆記】目錄(持續更新中...) Longest Increasing Subsequence Given an unsorted a