码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 112 - Tree Summing (数的求和!栈的应用!)

时间:2014-12-18 22:23:56      阅读:526      评论:0      收藏:0      [点我收藏+]

标签:uva   数据结构   acm         

UVA - 112

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

 Status

Description

bubuko.com,布布扣


 Tree Summing 

Background

LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest languages currently being used. Lists, which are the fundamental data structures in LISP, can easily be adapted to represent other important data structures such as trees.

This problem deals with determining whether binary trees represented as LISP S-expressions possess a certain property.

The Problem

Given a binary tree of integers, you are to write a program that determines whether there exists a root-to-leaf path whose nodes sum to a specified integer. For example, in the tree shown below there are exactly four root-to-leaf paths. The sums of the paths are 27, 22, 26, and 18.

bubuko.com,布布扣

Binary trees are represented in the input file as LISP S-expressions having the following form.

empty tree 		 ::= 		 ()

tree ::= empty treebubuko.com,布布扣 (integer treetree)

The tree diagrammed above is represented by the expression (5 (4 (11 (7 () ()) (2 () ()) ) ()) (8 (13 () ()) (4 () (1 () ()) ) ) )

Note that with this formulation all leaves of a tree are of the form (integer () () )

Since an empty tree has no root-to-leaf paths, any query as to whether a path exists whose sum is a specified integer in an empty tree must be answered negatively.

The Input

The input consists of a sequence of test cases in the form of integer/tree pairs. Each test case consists of an integer followed by one or more spaces followed by a binary tree formatted as an S-expression as described above. All binary tree S-expressions will be valid, but expressions may be spread over several lines and may contain spaces. There will be one or more test cases in an input file, and input is terminated by end-of-file.

The Output

There should be one line of output for each test case (integer/tree pair) in the input file. For each pair I,T (I represents the integer, T represents the tree) the output is the string yes if there is a root-to-leaf path in T whose sum is I and no if there is no path in T whose sum is I.

Sample Input

22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))
20 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))
10 (3 
     (2 (4 () () )
        (8 () () ) )
     (1 (6 () () )
        (4 () () ) ) )
5 ()

Sample Output

yes
no
yes
no

Source


Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Graph :: Special Graphs (Others) :: Tree
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Graph :: Special Graphs (Others) :: Tree
Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 4. Graph :: Special Graph - Tree
Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 2. Data Structures :: Binary Trees



我尼玛!这题真做的我整个人都不好了........


首先数据很坑!!

输入:

22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))
20 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))
10 (3
(2 (4 () () )
(8 () () ) )
(1 (6 () () )
(4 () () ) ) )
5 ()
0 ()
5 (5 () ())
5 ( 5 () () )
5 (1 (3 () ()) (4 () ()))
5 (18 ( - 13 ( ) ( ))())
0 (1 ()(-2 () (1()()) ) )
2 (1 () (1 () (1 () () ) ) )
10 (5 () (5 () (5 () (5 () (4 () () ) ) ) ) )
10 (5 () (5 () (5 () (5 ( 3 () () ) (4 () () ) ) ) ) )
20 (5 () (5 () (5 () (5 () (4 () () ) ) ) ) )

输出:

yes
no
yes
no
no
yes
yes
yes
yes
yes
no
no
no
no


居然尼玛有负号!!

还居然负号和数字之间有空格!!

我差点笑出声啊!!

哈哈哈哈哈%>_<%!!



AC代码:

#include <cstdio> 
#include <cstring>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;

int main()
{
	int n;
	char a[300];
	while(scanf("%d", &n) != EOF)
	{
		stack<int> snum;
		stack<char> sch;
		int sum = 0, flag = 0;
		int iswei = 0;  //判断是否到了叶子节点 
		gets(a);
		while(1)
		{
			for(int i=0; a[i] != '\0'; i++)
			{
				if( (a[i] <= '9' && a[i] >= '0') || a[i] == '-')
				{
					iswei = 0;
					int t = 0, f = 0;
					if(a[i] == '-')
					{
						while(a[i]>'9' || a[i] < '0') i++;
						f = 1;
					}
					while(a[i] <= '9' && a[i] >='0')
					{
						t = t*10 + a[i]-'0';
						i++;
					}
					i--;
					if(f) t = -t;
					sum += t;
					snum.push(t);
				}
				else if(a[i] == '(')
				{
					iswei++;
					sch.push(a[i]);
				}
				else if(a[i] == ')')
				{
					//printf("%d %d\n", sch.size(), snum.size());
					if(sch.size() == snum.size())
					{
						if(iswei == 2 && sum == n)  flag = 1;  //之前在这里就直接break了,导致后面没输入进去,纠结我好久 
						sum -= snum.top();
						snum.pop();
						iswei = 0;
					}
					sch.pop();
					//printf("%d\n", sum);
				}
			}
			if(sch.empty()) break;   //当所有()都匹配好了后即栈为空就说明树输出完毕,然后就跳出循环 
			else 
			{
				gets(a);
			}
		}
		if(flag == 1) printf("yes\n");
		else printf("no\n");
	}
	return 0;
}


另附大牛的代码(Orz...):

#include<iostream>
using namespace std;
bool ok;
bool tree_sum(int n,int sum)
{
    int v;
    char ch;
    cin>>ch;
    if(!((cin>>v)==0))
    {
        n+=v;
        bool t=tree_sum(n,sum)|tree_sum(n,sum);
        if(!ok&&!t)
            ok=(n==sum);
        cin>>ch;
        return true;
    }
    else
    {
        cin.clear();
        cin>>ch;
        return false;
    }
}
int main()
{
    // freopen("f:\\out.txt", "w", stdout);
    int sum;
    while(cin>>sum)
    {
        ok=false;
        tree_sum(0,sum);
        cout<<(ok?"yes":"no")<<endl;
    }
    return 0;
}


UVA - 112 - Tree Summing (数的求和!栈的应用!)

标签:uva   数据结构   acm         

原文地址:http://blog.csdn.net/u014355480/article/details/42011461

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!