UVA 11111 (暑期-线性表-G- Generalized Matrioshkas)

#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

int main() {
	long long arr[100010] = {0}, brr[100010][2] = {0}, n = 0;
	while (scanf("%lld", &arr[n]) != EOF) {
		if (getchar() == '\n') {
			int count = 0, kong = 0;
			for (int i = 0; i <= n; i++) {
				if (arr[i] < 0) //负数入栈
					brr[count++][0] = arr[i];
				else {			//正数出栈
					if (brr[count-1][0] + arr[i] == 0 ) {    	//判断是否匹配
						//计算该层里面包裹了多大
						brr[count-2][1] += brr[count-1][0];
						brr[count-1][1] = 0;
						count = count - 1;
						//判断要出栈的是否小于外层
						if (brr[count-1][1] <= brr[count-1][0] && count - 1 >= 0) {
							kong = 1;
							break;
						}
					}
					else {
						kong = 1;
						break;
					}
				}
			}
			if (count != 0 || kong)
				printf(":-( Try again.\n");
			else 
				printf(":-) Matrioshka!\n");
			memset(brr, 0, sizeof(brr));
			memset(arr, 0, sizeof(arr));
			n = -1;
		}
		n++;
	}
	return 0;
}

你可能感兴趣的:(UVA 11111 (暑期-线性表-G- Generalized Matrioshkas))