UVA11111Generalized Matrioshkas

来源:互联网 发布:程序员工作累吗? 编辑:程序博客网 时间:2024/06/03 17:56

UVA-11111

题意:大玩具里面可以塞小玩具,但是小玩具的体积和要小于 ( 没有等于!! ) 大玩具的体积。-n+n表示一个玩具,-7 -3 +3 +7表示一个体积为7的玩具里面塞来一个体积为3的玩具。判断给出的串合不合法。
解体思路:符号匹配 2.0。符号为- 时将n进栈,符号为+时出栈n,并在包含它的那个玩具的已装容量内增加n。当n出栈时判断容量是否大于已装容量。

/*************************************************************************    > File Name: UVA-11111.cpp    > Author: Narsh    >     > Created Time: 2016年07月16日 星期六 14时11分33秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;struct node{    int x,w;}t[30000];int l,n;char c;int main () {    while (scanf("%d%c",&n,&c) !=EOF) {        l = 1;        t[l].x =-n;        t[l].w = 0;        while (c !='\n') {            scanf("%d%c",&n,&c);            if (n < 0) {                t[++l].x=-n;                t[l].w=0;            }            if (n > 0) {                if ( t[l].x == n && t[l].w < t[l].x) {                    l--;                    t[l].w +=t[l+1].x;                }            }        }        if (!l) printf(":-) Matrioshka!\n");        else printf(":-( Try again.\n");    }}
0 0
原创粉丝点击