ACM Learning Process--uva11111 Generalized Matrioshkas (stack)

Source: Internet
Author: User

Description

problem b-generalized Matrioshkas
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 be true,!--MATH $n _1+ n_2+ \dots + n_ R m$--> n 1 + 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 with the negative one occurring in the sequence first, 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:

  • !--MATH \begin{displaymath}-9\quad-7\quad-2\quad 2\quad-3\quad-1\quad-2\quad 2\quad 1\quad 3\quad 7\quad 9\end{displaymath}-->

          -9     -7    -2     2     -3     -1     -2     2    1    3    7     9

     

    because toy 2 is bigger than toy 1 and cannot be allocated inside it.

     

  • !--MATH \begin{displaymath}-9\quad-7\quad-2\quad 2\quad-3\quad-2\quad-1\quad 1\quad 2\quad 3\quad 7\quad -2\quad 2\quad 9\end{displaymath}-->

          -9     -7      -2    2     -3     -2      -1&NBSP;&NBSP;&NBSP;&NBSP;1&NBSP;&NBSP;&NBSP;&NBSP;2&NBSP;&NBSP;&NBSP;&NBSP;3&NBSP;&NBSP;&NBSP;&NBSP;7      -2    2    9

     

    because 7 and 2 may is not allocated together inside 9.

     

  • -9-7-2 2-3-1-2 3 2 1 7 9

    Because there is a nesting problem within toy 3.

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.

Follow the questions as follows:
1, negative numbers directly into the stack.
2, top for 0 directly into the stack.
3, if it is positive:
1 ·, can be combined with the top of the stack, so that the top element of the stack OK value is 1 (the OK value starts with 0), and by negative positive. The positive value is not in the stack.
2 ·, can be combined with the second element of the top of the stack, and can tolerate the stack top OK element (not OK does not meet the conditions), pop up the top element of the stack, at this time the top element of the stack OK value to 1, by negative positive.
3 ·, after the above process, the continuous OK value of the combination. The Val value is added.
4 ·, to the non-union of positive numbers, according to the requirements of the topic is necessarily bad value, can be stacked or not into the stack, the results of the judgment does not affect.
4, finally as long as the stack only one element, and its ok value is 1, the description is good, the rest is bad.


Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> #include <set> #include <map> #include <vector> #include <queue>    #include <string> #define INF 0x3fffffff#define eps 1e-10using namespace std;struct node{bool OK; int Val;};    Node Stack[10000];int top;int Input () {int v;    Char ch;    top = 0;    for (;;)        {if (scanf ("%d", &v) = = EOF) return-1;        ch = getchar ();            if (v < 0) {Stack[top].ok = 0;            Stack[top].val = v;        top++;            } else if (top = = 0) {Stack[top].ok = 0;        Stack[top].val = v;            } else if (Stack[top-1].val = =-V) {Stack[top-1].ok = 1;        Stack[top-1].val = v; } else if (Top > 1 && stack[top-1].ok = = 1 && stack[top-2].val = =-V &&Amp            Stack[top-1].val < v) {top--;            Stack[top-1].ok = 1;        Stack[top-1].val = v;        } while (Top > 1 && stack[top-1].ok = = 1 && Stack[top-2].ok = = 1)            {Stack[top-2].val + = Stack[top-1].val;        top--;            } if (ch = = ' \ n ') {if (top = = 1 && Stack[top-1].ok = = 1) return 1;        else return 0;    }}}void qt () {int ans;    for (;;)        {ans = Input ();        if (ans = =-1) break; if (ans = = 0) printf (":-(        Try again.\n "); else printf (":-)    Matrioshka!\n ");    }}int Main () {//freopen ("test.txt", "R", stdin);    QT (); return 0;}

ACM Learning Process--uva11111 Generalized Matrioshkas (stack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.