zoukankan      html  css  js  c++  java
  • 1045 Favorite Color Stripe [动态规划/最长不下降子序列]

    在这里插入图片描述

    #include<iostream>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<string>
    #include<math.h>
    #include<algorithm>
    #include<map>
    #include<cstring>
    #include<set>
    using namespace std;
    const int maxn = 10001;
    int a[maxn], dp[maxn];
    int hashtable[maxn];
    int main()
    {
    	int n, m, x;
    	cin >> n >> m;
    	memset(hashtable, -1, sizeof(hashtable));
    	for (int i = 0; i < m; i++)
    	{
    		cin >> x;
    		hashtable[x] = i;
    	}
    	int l, num = 0;
    	cin >> l;
    	for (int i = 0; i < l; i++)
    	{
    		cin >> x;
    		if (hashtable[x] >= 0)
    		{
    			a[num++] = hashtable[x];
    		}
    	}
    	int ans = -1;
    	for (int i = 0; i < num; i++)
    	{
    		dp[i] = 1;
    		for (int j = 0; j < i; j++)
    		{
    			if (a[j] <= a[i] )
    			{
    				dp[i] = dp[j] + 1;
    			}
    		}
    		ans = max(ans, dp[i]);
    	}
    	cout << ans << endl;
    }
    

    最长公共子序列

    #include<iostream>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<string>
    #include<math.h>
    #include<algorithm>
    #include<map>
    #include<cstring>
    #include<set>
    using namespace std;
    const int maxn = 10001;
    int a[maxn], b[maxn], dp[maxn][maxn];
    int main()
    {
    	int n, m;
    	cin >> n >> m;
    	for (int i = 1; i <= m; i++)
    	{
    		cin >> a[i];
    	}
    	int l; cin >> l;
    	for (int i = 1; i <= l; i++)
    	{
    		cin >> b[i];
    	}
    	for (int i = 0; i <= m; i++)
    	{
    		dp[i][0] = 0;
    	}
    	for (int j = 0; j <= l; j++)
    	{
    		dp[0][l] = 0;
    	}
    	int num = 0;
    	for (int i = 1; i <= m; i++)
    	{
    		for (int j = 1; j <= l; j++)
    		{
    			int MAX = max(dp[i - 1][j], dp[i][j - 1]);
    			if (a[i] == b[j])
    			{
    				dp[i][j] = MAX + 1;
    			}
    			else
    			{
    				dp[i][j] = MAX;
    			}
    		}
    	}
    	cout << endl;
    	cout << dp[m][l] << endl;
    	return 0;
    
    }
    
  • 相关阅读:
    openstack nova 基础知识——Quota(配额管理)
    02python注释
    01使用Pycharm编写第一个python程序
    35Angular实现一个todoList
    34Angular实现搜索缓存数据功能
    33Angular实现人员登记系统(表单元件的双向数据绑定)
    32练习
    31封装一个网络请求的服务
    30服务(练习)
    29服务
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811989.html
Copyright © 2011-2022 走看看