1045. Favorite Color Stripe (30)

本文介绍了一种求解最长非连续子序列的算法实现,通过C++代码展示了如何处理输入数据并找到符合条件的最长子序列。该算法适用于解决特定类型的序列匹配问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

考察最长非连续子序列,有点变种

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

int n, m, l;
typedef struct Node
{
	int len, pre;
}Node;
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		scanf("%d",&m);
		vector<int> fc(m);
		map<int,int> color2id;
		for(int i = 0; i < m; ++i)
		{
			int c;
			scanf("%d",&c);
			fc[i] = c; color2id.insert(make_pair(c, i));
		}
		//
		scanf("%d",&l);
		vector<int> a;
		for(int i = 0; i < l; ++i)
		{
			int c;
			scanf("%d",&c);
			map<int,int>::iterator it = color2id.find(c);
			if(it!=color2id.end())
				a.push_back(it->second);
		}
		//get the maximum non-continuous subsequence
		int realn = a.size();
		vector<int> d(realn, 1);
		if(realn == 0)
			printf("0\n");
		else
		{
			//d[0] = 1; //d[0].pre = 0;
			for(int i = 1; i < realn; ++i)
			{
				//get the maximum one
				for(int j = 0; j < i; ++j)
				{
					if(a[i] >= a[j])
						d[i] = max(d[i], d[j]+1);
				}
			}
			//then get the max
			int mmax = 0;
			for(int i = 0; i < realn; ++i)
				mmax = max(d[i], mmax);
			printf("%d\n",mmax);
		}

	}
	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值