网易游戏2016校园招聘“游戏研发&平台开发”在线笔试——A题 Amusing Digits

注意,lz并没有参加在线笔试,只是拿来练习一下,由于在hihocoder上并不能提交,只能用样例测试,所以不保证答案完全正确

题目:http://hihocoder.com/contest/ntest2015septdev/problem/1

分析:模拟题,贪心即可,对每一个9尝试找出最靠前的7、0、6,使得其满足9706的子串顺序,时间复杂度是O(N),空间复杂度是O(N)

#include <cstdio>
#include <queue>
using namespace std;

int main()
{
	int test;
	scanf("%d", &test);
	while(getchar() != '\n') ;

	while(test--){
		queue<int> nine, seven, zero, six;
		int i = 0, c;
		for(; (c = getchar()) != '\n'; ++i){
			if(c == '9') nine.push(i);
			else if(c == '7') seven.push(i);
			else if(c == '0') zero.push(i);
			else if(c == '6') six.push(i);
		}
		
		int tot = 0;
		for(; !nine.empty(); ++tot){
			int x = nine.front(); nine.pop(); //printf("9 at %d\n", x);
			while(!seven.empty() && x > seven.front()) seven.pop();
			if(seven.empty()) break;

			x = seven.front(); seven.pop(); //printf("7 at %d\n", x);
			while(!zero.empty() && x > zero.front()) zero.pop();
			if(zero.empty()) break;

			x = zero.front(); zero.pop(); //printf("0 at %d\n", x);
			while(!six.empty() && x > six.front()) six.pop();
			if(six.empty()) break;

			x = six.front(); six.pop(); //printf("6 at %d\n", x);
		}
		printf("%d\n", tot);
	}

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值