uva 11111

俄罗斯套娃。。。怎么这么多题的背景都是俄罗斯套娃。。。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sstream>
#include <memory.h>
#include <stack>

using namespace std;

struct Toy
{
	int id;
	int size;
};
const int maxn = 50000 + 30;
int stPoint = 0, temp = 0, toys[maxn], parent[maxn], toyCnt = 0;
string str;
bool flag = true;
stack<Toy> toyStack;
Toy toybp[maxn];

void check()
{
	while (!toyStack.empty())
		toyStack.pop();

	for (int i = 0; i < stPoint; i++)
	{
		if (toys[i] < 0)
		{
			Toy t;
			t.id = toyCnt++;
			t.size = -toys[i];
			toybp[t.id] = t;
			toyStack.push(t);
		}
		else
		{
			if (!toyStack.empty() && toyStack.top().size == toys[i])
			{
				Toy t = toyStack.top();
				toyStack.pop();
				if (!toyStack.empty())
					parent[t.id] = toyStack.top().id;
				else
					parent[t.id] = -13;
			}
			else
				flag = false;
		}//else
	}

	if (!toyStack.empty())
		flag = false;
}

int main()
{
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	while (getline(cin, str))
	{
		stringstream ss(str);
		stPoint = 0;
		flag = true;
		memset(parent, -1, sizeof(parent));
		toyCnt = 0;

		while (ss >> temp)
			toys[stPoint++] = temp;
		check();
		if (flag)
		{
			for (int i = 0; i < toyCnt; i++)
			{
				int parIndex = parent[i];
				if (parIndex == -13)
					continue;
				else
				{
					int sum = 0;
					for (int j = 0; j < toyCnt; j++)
					{
						if (parent[j] == parIndex)
							sum += toybp[j].size;
					}
					if (sum >= toybp[parIndex].size)
						flag = false;
				}//else
			}//for
		}
		printf("%s\n", flag ? ":-) Matrioshka!" : ":-( Try again.");
	}//while
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值