Problem b-generalized Matrioshkas |
Vladimir worked for years making Matrioshkas, those nesting dolls, certainly represent truly Russian craft. A Matrioshka is a doll the May being opened in the halves, so the one finds another doll inside. Then this doll is opened to find another one inside it. This can is repeated several times, till a final doll-that cannot be Opened-is reached.
Recently, Vladimir realized, the idea of nesting dolls might is generalized to nesting toys. Indeed, he has designed toys, and contain toys but is a more general sense. One of these toys May is opened in, halves and it may has more than one toy inside it. That's the new feature that Vladimir wants to introduce in its new line of toys.
Vladimir have developed a notation to describe about nesting toys should be constructed. A toy is represented with a positive integer, and according to its size. More precisely:if when opening the toy represented by m we find the toys represented by n 1, N 2, n R, it must is true that n 1 + Em>n 2 + ... + n r < m . And if the the case, we say that toy m contains directly the toys n 1, n 2, ..., n R. It should be clear this toys that is contained in any of the toys n 1, < Em>n 2, n R is 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:
a1
a2 ...
aN
such that toy K was represented in the sequence with the integers- k and K, W ith the negative one occurring in the sequence first and the positive one.
For example, the sequence
-9 -7 -2 2 -3 -2 -1 1 2 3 7 9
Represents a generalized Matrioshka conformed by six toys, namely, 1, 2 (twice), 3, 7 and 9. Note that toy 7 contains directly toys 2 and 3. Note that the first copy of toy 2 occurs left from the second one and that the second copy contains directly a toy 1. It would is wrong to understand that the first -2 and the last 2 should be paired.
On the other hand, the following sequences does not describe generalized Matrioshkas:
Your problem is-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, and each of the one with the absolute value less than 107.
Output
Output texts for each of the input case was presented in the same order that input is read.
For each test case the answer must is a line of the form
:-) matrioshka!
If the design describes a generalized Matrioshka. 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 4 5 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.
#include <iostream>#include<stack>#include<cstring>#include<cstdio>#include<string>#include<algorithm>#include<queue>#include<Set>#include<map>#include<fstream>#include<stack>#include<list>#include<sstream>using namespacestd;/*Test Instructions: A doll that can nest dolls, then nested inside the doll can continue to nest dolls. Then the size of the doll that is directly nested inside (the inner layer) must not exceed the dimensions of the outside. (Pay special attention to input)*/#defineMS (arr, Val) memset (arr, Val, sizeof (arr))#defineN 50000#defineINF 0X3FFFFFFF#defineVint vector<int>#defineSint Set<int>#defineMint Map<int, int>#defineLint list<int>#defineSch stack<char>intSeq[n], Sum[n], top, A;stringLine ;voidinit () {MS (SUM,0); Top= -1;}BOOLjudge () {init (); Istringstream ISS (line); while(iss>>a) {if(A <0) {sum[++top]-= A;//records the current layer's andSeq[top] =A; } Else { if(Top <0)//sequence is empty { return false; } if(-seq[top]! = a)//Unequal { return false; } if(-seq[top] <= sum[top +1])//the current number is not greater than the inner layer and { return false; } //satisfies the condition, can matchSum[top +1] =0;//inner and empty, without affecting back operationtop--; } } returnTop <0?true:false;}intMain () { while(Getline (CIN, line)) {cout<< (judge)?":-) matrioshka!":":-( Try again.") <<Endl; } return 0;}