ZOJ 3829 Known Notation

搞一手波兰表达式。

显然的策略:1.数字个数cnt必须大于等于*的个数数opt+1,cnt=opt+1时,前面放2个数字,后面*和数字交替放,这是极限了。

2.如果当前扫到的*号太多导致前面没数字可算了,我们从尽可能后面的地方找数字和它交换,因为数字越前面越好,*号越后面越好,合法的可能性更大

所以就记录一个当前前面的数字个数res,以及从后往前的指针numind,来从后往前找数字,因为每次交换要交换尽可能后面的数字。复杂度就是O(n)的了。

#include<bits/stdc++.h>
#define maxl 1010

using namespace std;

int n,opt,cnt,ans;
char s[maxl];

inline void prework()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	opt=0;cnt=0;
	for(int i=1;i<=n;i++)
	if(s[i]=='*')
		opt++;
	else
		cnt++;
}

inline void mainwork()
{
	int res;
	if(opt+1>cnt)
		ans=opt+1-cnt,res=opt+1-cnt;
	else
		ans=0,res=0;
	int numind=n;
	for(int i=1;i<=n;i++)
	if(s[i]=='*')
	{
		if(res>1)
			res--;
		else
		{
			while(s[numind]=='*')
				numind--;
			swap(s[numind],s[i]);
			numind--;
			res++;ans++;
		}
	}
	else
		res++;
		
}

inline void print()
{
	printf("%d\n",ans);
}

int main()
{
	int t;
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值