Problem B - Generalized Matrioshkas
Problem B - Generalized Matrioshkas |
Vladimir worked for years making matrioshkas, those nesting dolls that certainly represent truly Russian craft. A matrioshka is a doll that may be opened in two halves, so that one finds another doll inside. Then this doll may be opened to find another one inside it. This can be repeated several times, till a final doll -that cannot be opened- is reached.
Recently, Vladimir realized that the idea of nesting dolls might be generalized to nesting toys. Indeed, he has designed toys that contain toys but in a more general sense. One of these toys may be opened in two halves and it may have more than one toy inside it. That is the new feature that Vladimir wants to introduce in his new line of toys.
Vladimir has developed a notation to describe how nesting toys should be constructed. A toy is represented with a positive integer, according to its size. More precisely: if when opening the toy represented by m we find the toys represented by n1, n2, ..., nr, it must be true that n1 + n2 + ... + nr < m. And if this is the case, we say that toy mcontains directly the toys n1, n2, ..., nr . It should be clear that toys that may be contained in any of the toys n1, n2, ...,nr are not considered as directly contained in the toy m.
A generalized matrioshka is denoted with a non-empty sequence of non zero integers of the form:
For example, the sequence
On the other hand, the following sequences do not describe generalized matrioshkas:
-
-9 -7 -2 2 -3 -1 -2 2 1 3 7 9because toy 2 is bigger than toy 1 and cannot be allocated inside it.
-
-9 -7 -2 2 -3 -2 -1 1 2 3 7 -2 2 9because 7 and 2 may not be allocated together inside 9.
-
-9 -7 -2 2 -3 -1 -2 3 2 1 7 9because there is a nesting problem within toy 3.
Your problem is to write a program to help Vladimir telling good designs from bad ones.
Input
The input file contains several test cases, each one of them in a separate line. Each test case is a sequence of non zero integers, each one with an absolute value less than 107.
Output
Output texts for each input case are presented in the same order that input is read.
For each test case the answer must be a line of the form
:-) Matrioshka!
if the design describes a generalized matrioshka. In other case, the answer should be of the form
:-( Try again.
Sample Input
-9 -7 -2 2 -3 -2 -1 1 2 3 7 9 -9 -7 -2 2 -3 -1 -2 2 1 3 7 9 -9 -7 -2 2 -3 -1 -2 3 2 1 7 9 -100 -50 -6 6 50 100 -100 -50 -6 6 45 100 -10 -5 -2 2 5 -4 -3 3 4 10 -9 -5 -2 2 5 -4 -3 3 4 9
Sample Output
:-) Matrioshka! :-( Try again. :-( Try again. :-) Matrioshka! :-( Try again. :-) Matrioshka! :-( Try again.
方法:
和括号匹配问题差不多。
基本想法是:
1.依次读取数字(方便起见设为x), 如果x < 0,则入栈,否则(即x > 0)转到2
2.若栈顶元素大于0,则出栈并计算出栈元素的和(方便起见设为s),直到栈顶元素小于0。
3.若s > x 或 栈空 或 栈顶元素不是x 的相反数, 则返回0,退出。
4.出栈栈顶元素(x的相反数),入栈 s。
5.若还有元素可读,转到 1.
6.若栈中还剩一个大于0的元素(即最后入栈的s),返回 1,否则返回 0.
#include #define N 10000 int judge(int a[], int n) { int i, top, s, f; top = 0; f = 1; for(i = 0; i < n; i++){ if(a[i] < 0) a[top++] = a[i]; else{ s = 0; while(top > 0 && a[top-1] > 0){ s += a[top-1]; top--; } if(top == 0 || a[top-1] + a[i] != 0 || s >= a[i]){ f = 0; break; } top--; a[top++] = a[i]; } } if(top != 1 || a[top-1] < 0 || i < n) f = 0; return f; } int str2int(char s[], int a[]) { int i, j, sign, t; t = 0; j = 0; sign = 1; for(i = 0; s[i] != '\0'; i++){ if(s[i] == '-') sign = -1; else if(s[i] >= '0' && s[i] <= '9') t = t * 10 + s[i] - '0'; else if(s[i] == ' '){ a[j++] = t * sign; t = 0; sign = 1; while(s[i+1] == ' ') i++; } } a[j++] = t; return j; } int main() { int n, a[N]; char s[5*N]; while(gets(s) != NULL){ n = str2int(s, a); if(judge(a, n)) printf(":-) Matrioshka!\n"); else printf(":-( Try again.\n"); } return 0; }