PAT 1045. Favorite Color Stripe (30)

1045. Favorite Color Stripe (30)
//按照普通的算法实现,居然超时还有错误,不明白啊???
//wrong answer
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	int m;
	int l;
	int color[205];
	//vector<int> stripe;
	int stripe[10005];

	scanf("%d", &n);
	scanf("%d", &m);

	int i;
	for (i=0;i<m;i++)
	{
		/*cin >> color[i];*/
		scanf("%d", &color[i]);
	}

	scanf("%d", &l);

	int tmp;

	for (i = 0;i<l;i++)
	{
		//cin >> tmp;
		scanf("%d", &stripe[i]);
	}


	int maxlength = 0;
	int currlength = 0;
	
	int BeforeMaxIndex = -1;
	int CurIndex;
	int tmpIndex;

	int j;
	for (i = 0;i<l;i++)
	{
		int* temp = find(color, color + m, stripe[i]);
		CurIndex = temp - color;
		if (CurIndex < 0 || CurIndex >= m)
		{
			continue;
		}
		if (CurIndex >= BeforeMaxIndex)
		{
			currlength++;
			BeforeMaxIndex = CurIndex;
		}
		else
		{
			currlength++;
			BeforeMaxIndex = CurIndex;
			for (j = i-1;j>=0;j--)
			{
				temp = find(color, color+m, stripe[j]);
				tmpIndex = temp - color;

				if (tmpIndex < 0 || tmpIndex >= m)
				{
					continue;
				}

				if (tmpIndex > CurIndex)
				{
					currlength--;
				}
			}
		}

		if (currlength > maxlength )
		{
			maxlength = currlength;
		}
	}

	printf("%d", maxlength);
	return 0;
}

//1045. Favorite Color Stripe (30)
//accept
//动态规划!!!!
//注意前后之间到底关联性是什么 如何正确添加下一个值。。。。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	int m;
	int l;
	int color[205];
	int stripe;


	int num[205] = {0};

	scanf("%d", &n);
	scanf("%d", &m);

	int i;
	for (i=0;i<m;i++)
	{
		scanf("%d", &color[i]);
	}

	scanf("%d", &l);

	int maxlength = 0;
	int curLength = 0;
	int j = 0;
	int maxtmp = 0;

	for (i = 0;i<l;i++)
	{
		scanf("%d", &stripe);

		int* tmp = find(color, color+m, stripe);
		int index = tmp - color; 
		if ( index < 0 || index >= m)
		{
			continue;
		}

		maxtmp = 0;
		for (j = 0;j<=index;j++)
		{
			if (j == 0)
			{
				maxtmp = num[color[j]];
			}

			if (maxtmp < num[color[j]])
			{
				maxtmp = num[color[j]];
			}
		}

		curLength = maxtmp + 1;

		num[stripe] = curLength;

		if (i == 0||curLength > maxlength)
		{
			maxlength = curLength;
		}

		curLength = 0;
	}

	printf("%d", maxlength);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值