您的位置:首页 > 其它

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

2015-09-16 20:15 543 查看
注意,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: