zoukankan      html  css  js  c++  java
  • LCS and LIS

    LCS

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    int n,m;
    char s[1005],t[1005];
    int dp[1005][1005];
    
    int main()
    {
    	//scanf("%d %d",&n,&m);
    	scanf("%s %s",s,t);
    	
    	n = strlen(s);
    	m = strlen(t);
    	
    	for(int i=0;i<n;i++){
    		for(int j=0;j<m;j++){
    			if(s[i] == t[j]){
    				dp[i+1][j+1] = dp[i][j] + 1;
    			}else{
    				dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
    			}
    		}
    	}
    	
    	cout<<dp[n][m]<<endl;
    	
    	return 0;
    }

    LIS

    #include<bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    typedef long long ll;
    
    int dp[105];
    int a[105];
    
    int main()
    {
    	int n;
    	scanf("%d",&n);
    	for(int i=0;i<n;i++) scanf("%d",&a[i]);
    	
    	int res = 0;
    	for(int i=0;i<n;i++){
    		dp[i] = 1;
    		for(int j=0;j<i;j++){
    			if(a[j] < a[i]){ //如果是包括相等,就改为<= 
    				dp[i] = max(dp[i],dp[j]+1);
    			}
    		}
    		res = max(res,dp[i]); 
    	}
    	cout<<res<<endl;
    
    	return 0;
    }


  • 相关阅读:
    正则表达式
    匿名函数作业
    内置函数&匿名函数
    模拟面试题一
    迭代器
    生成器
    装饰器
    函数
    疑问?
    3,app信息抽取
  • 原文地址:https://www.cnblogs.com/pearfl/p/10733159.html
Copyright © 2011-2022 走看看