codeforces 917A

53 篇文章 0 订阅
38 篇文章 0 订阅

考场上写挂了一个小地方,然后WA了。。。还好躺一手hls,复制D的代码改变量名交了,还上了点分,我还是菜啊

据说正着贪一遍反着贪一遍有一个行就ans++,然后这样就可以A了,甚至还有直接贪A掉的。还不知道什么操作

top[j]表示从i开始到j还有多少个没有被 ) 匹配掉的 (

cnt[j]表示从i开始到j还有多少个没有 ) 和 ? 匹配掉的 (

top2[j]表示从i开始到j还有多少个不需要和 ( 匹配的 ?

这样就可以DP了,难道这就是贪心?

#include<cstdio>
#include<cstring>
#define maxl 5010

int top[maxl],n,top2[maxl],ans;
int sta[maxl],cnt[maxl];
char s[maxl];
bool vis[maxl][maxl];

int main()
{			
	memset(vis,true,sizeof(vis));
	scanf("%s",s+1);;
	n=strlen(s+1);
	bool flag=false;
	for(int i=1;i<=n;i++)
	{	
		memset(top,0,sizeof(top));
		memset(top2,0,sizeof(top2));
		memset(cnt,0,sizeof(cnt));
		memset(sta,0,sizeof(sta));
		for(int j=i;j<=n;j++)
		{
			if(s[j]=='(')
			{
				top[j]=top[j-1]+1;sta[top[j]]=j;
				cnt[j]=cnt[j-1]+1;top2[j]=top2[j-1];
				vis[i][j]=false;
				continue;
			}
			if(s[j]==')')
			{
				if(top[j-1]>0)
				{
					if(cnt[j-1]<cnt[sta[top[j-1]]])
					{
						cnt[j]=cnt[j-1];
						top2[j]=top2[j-1]+1;
						if(cnt[j]>0 && top2[j]>0)
							top2[j]--,cnt[j]--;
						top[j]=top[j-1]-1;
					}
					else
					{
						cnt[j]=cnt[j-1]-1;
						top2[j]=top2[j-1];    
						top[j]=top[j-1]-1;
					}
					if((top[j]==0 && top2[j]%2==0) || 
					(cnt[j]==0 && top2[j]%2==0))
						ans++;
				}
				else
				{
					if(top2[j-1]>0)
					{
						cnt[j]=cnt[j-1];//0
						top2[j]=top2[j-1]-1;
						top[j]=top[j-1];//0
					}
					else
						break;
					if(top2[j]%2==0)
						ans++;
				}
			}
			if(s[j]=='?')
			{
				if(cnt[j-1]>0)
				{
					cnt[j]=cnt[j-1]-1;
					top2[j]=top2[j-1];//0
					top[j]=top[j-1];
				}
				else
				{
					cnt[j]=cnt[j-1];   //0
					top2[j]=top2[j-1]+1;
					top[j]=top[j-1];
				}
				if((top[j]==0 && top2[j]%2==0) || 
					(cnt[j]==0 && top2[j]%2==0))
					ans++;
			}
		}
	}
	printf("%d",ans);
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值