Talk about:
Test instructions is a large integer containing several small integers, and a small integer larger than a large integer. Also small integers may contain other small integers, and so on. Ask to determine if the entire containment relationship is correct. Just the description of the topic is not very clear. First, give you a sequence, for example:-9-7-2 2-3-1-2 2 1 3 7 9 where 9 is between 9 and 9 and contains only 7. If the sequence is-9-7 7-2 2 9 9 contains 7 and 22 numbers, which is obviously illegal. The solution is simply to build two stacks, one to store the input number, and the other to mark the inclusion of the integer will not be illegal.
Source:
#include <stdio.h> #define MAXN 10000000+5int stack[maxn];//store data int calculate[maxn];//tag data is reasonable int main () { int p,wrong,val; char c; Freopen ("Data", "R", stdin); while (1) { p=wrong=0; do{ scanf ("%d%c", &val,&c); if (val<0) {//Insert negative stack[p]=calculate[p]=val; p++; } else{//Insert Positive if (stack[p-1]!=-val) {//If the top of the current stack does not match wrong=1; } p--; if (p-1>=0) { calculate[p-1]+=val;//is not empty on the stack, modify if (calculate[p-1]>=0) the stack for which the token data is reasonable wrong=1 ; } } } while (c!= ' \ n '); if (Wrong| | p!=0) printf (":-( Try again.\n "); else printf (":-) Matrioshka!\n "); if ((C=getchar ()) ==eof) break ; else ungetc (C,stdin); } return 0;}
Generalized Matrioshkas UVA 11111