棧題,啥也不說了,建立兩個棧,一個棧保存當前玩具的大小,一個棧保存當前玩具裏面嵌套的玩具的大小
直曬ac代碼
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAXN = 100000 + 10;
int stacks1[MAXN],stacks2[MAXN];
int top1,top2;
int main()
{
char s[MAXN];
int num,u,sum;
//freopen("in.txt","r",stdin);
while(gets(s)){
top1 = top2 = 0;
int ok = 1;
int len = strlen(s);
num = (len+1)*3/2;
for(int i = 0;i<len;i++){
sscanf(s+i,"%d",&u);
int v = abs(u),bit = 0;
while(v>0){bit++;v/=10;}
if(u<0)i+=bit+1;
else i+=bit;
if(u<0){stacks1[top1++] = u;stacks2[top2++] = 0;}
else if(u>stacks2[top2-1] && u == -stacks1[top1-1]){
top1--;
if(top1>0)stacks2[top1-1]+=u;
top2--;
}
else{
printf(":-( Try again.\n");ok = 0;
break;
}
}
if(ok){
if(top1 == 0)printf(":-) Matrioshka!\n");
else printf(":-( Try again.\n");
}
}
return 0;
}