洛谷P1684 考验(贪心)

贪心

尽可能的在已经连接起来的诗歌后面接上新的诗歌

新建一个last存储上次匹配串的末尾的后一个字符

每当当前点的位置到last的长度大于四,有可能成为新串的时候

暴力判断是否可以成为诗歌,若成功更新答案和last

我们用f[i]表示以i结尾的诗歌数

所以每次更新是f[last-1]+1

若不成功f[i]=f[i-1]

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 4005;
ll num[maxn];
ll f[maxn];
ll n;
bool flag(ll l,ll r)
{
	int t=0;
	int nn[maxn];
	nn[4]=num[r];
	for(int i=l;i<r;i++)
	{
		nn[++t]=num[i];
		for(int j=l;j<r;j++)
		{
			if(j==i)continue;
			nn[++t]=num[j];
			for(int k=l;k<r;k++)
			{
				if(k==j||k==i)continue;
				nn[++t]=num[k];
				if((nn[1]==nn[2]&&nn[3]==nn[4])||((nn[1]==nn[4]&&nn[2]==nn[3])||(nn[1]==nn[3]&&nn[2]==nn[4])))return true;
				t--;
			} 
			t--;
		}
		t--;
	}
	return false;
}
int main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
	scanf("%lld",&num[i]);
	f[0]=0;
	ll last=1;
	for(int i=1;i<=n;i++)
	{
		if(i-last+1>=4)
		{
			if(flag(last,i))
			{
				f[i]=max(f[i],f[last-1]+1);
				last=i+1;
			}
			else f[i]=f[i-1];
		}
		else f[i]=f[i-1];
	}
	cout<<f[n];
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值