Topic Link:
Http://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&category=103&page=show_problem &problem=2052
Type of topic: data structure, linked list
The main effect of the topic:
This question is more difficult to understand, read several changes before understand. There is a doll can be nested dolls, and then nested inside the doll can continue to nest dolls.
The size of the doll that is directly nested inside (the inner layer) is then required to not exceed the size of the outside. For example,-3-2 2 3, representing two layers,-3 and 3 indicate a nesting (the doll has a size of 3, and negative numbers must appear first on the left), when-2 and 2 represent a doll of size 2. Another example-5-2 2-1 1 5, indicating 3 dolls, 5 nested these 2 and 1. When there are more nesting levels, such as-9-7-2 2-3-2-1 1 2 3 7 9, which means 9 is nested 7. Then 7 is nested 2 (left -2,2) and 3, and 3 is nested 2 (right) 2 , 2), this 2 is nested 1. As long as the adjacent level, the size of the inner layer is less than (not equal to) the size of the outer layer satisfies the condition.
Ideas for solving problems:
This is a bit like "bracket nesting". The first is the input of the topic, because the beginning did not adopt a good input scheme, resulting in WA countless times, wasted a few hours. Then the topic needs to be noticed in some places: 1. If the number of dolls is odd, then the error is directly judged; 2. Positive number and negative numbers do not want to wait, is also wrong; 3. If
A positive number appears before the corresponding negative number, and is also wrong.
This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45536.htm
The method I used was to open an array level to hold the doll's capacity at all levels and then process it every time I encountered a ') ', subtracting the current level's "outer" capacity from the size of the hierarchy. When there is a level of 0 or negative, it can be judged to be wrong. The most needed attention is how to handle and judge which layer is at the current level.
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
-10-5-2 2 5-4-3 3 4-9-5-
2 2 5-4-3 3 4-9
Sample output:
:-) matrioshka!
:-( Try again.
:-( Try again.
:-) matrioshka!
:-( Try again.
:-) matrioshka!
:-( Try again.
#include <cstdio> #include <iostream> #include <cstring> #include <stack> #include <CMATH&G
T
using namespace Std;
String str;
int level[30000], arr[30000], Numdoll, Cntneg, Cntpos, N;
stack<int>st;
BOOL Solve () {if (Numdoll%2==1 | | cntneg!=cntpos) return FALSE;
int i,j,index=0;
if (numdoll==2) {if (arr[0]<0 && arr[0]+arr[1]==0) return true;
return false;
while (!st.empty ()) St.pop ();
For (i=0 i<numdoll; ++i) {if (arr[i]<0) {St.push (arr[i]);
level[index++] = ABS (Arr[i]); else if (arr[i]>0) {if (St.empty () | | ABS (ST.TOP ())!= Arr[i]) return False
;
if (St.size () ==1) {if (level[0] <=0) return false;
St.pop ();
} else{St.pop ();
--index;
Level[st.size ()-1]-= arr[i];
if (Level[st.size ()-1] <= 0) {return false;
}}} return true;
void input () {arr[0] = n;
while (GetChar ()!= ' \ n ') {scanf ("%d", &arr[numdoll++]);
};
int main () {freopen ("Input.txt", "R", stdin);
BOOL Flag=true;
Cntneg=0, cntpos=0;
numdoll=1;
while (~SCANF ("%d", &n)) {input (); if (solve ()) printf (":-)
Matrioshka!\n "); else printf (":-(
Try again.\n ");
cntneg=0;
cntpos=0;
numdoll=1;
return 0; }