动态规划 | 对输入进行hash处理的LIS 1045

把序列M处理为有序序列,并且M不存在的序列要在A中删除。

对A进行了处理之后,执行LIS的操作(O(N^2)复杂度)。当然可以优化为对数复杂度的,不过pat不卡这个。

LCS解法:动态规划 | 保留重复元素的LCS 1045

AC代码:

#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>


#define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 10010
#define MAX (1<<30)-1
#define V vector<int>

using namespace std;

int hashTable[210];    //将输入颜色映射为递增序列 
int A[10010];        //处理后的颜色序列(用LIS对这个数组进行求解 
int dp[10010];        //dp[i]记录 0 ~ i 最多的递增序列个数 

int main(){
//    freopen("1045.txt","r",stdin);
    int i,N,M,L,x,j;
    memset(hashTable,-1,sizeof hashTable);
    I("%d",&N);
    I("%d",&M);
    FF(i,M){
        I("%d",&x);
        hashTable[x]=i;
    }
    I("%d",&L);
    int num=0;    //处理后A数组颜色的总和 
    FF(i,L){
        I("%d",&x);
        if(hashTable[x]>=0){
            A[num++]=hashTable[x];
        }
    }
    int ans=0;
    FF(i,num){
        dp[i]=1;
        FF(j,i){
            if(A[j]<=A[i]){
                dp[i]=max(dp[i],dp[j]+1);
            }
        }
        ans=max(ans,dp[i]);
    }
    O("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/TQCAI/p/8571481.html