24h購物| | PChome| 登入
2012-02-26 08:47:22| 人氣3,947| 回應0 | 上一篇 | 下一篇

[UVA] 112 - Tree Summing

推薦 0 收藏 0 轉貼0 訂閱站台

 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.

picture25

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

 
empty tree 		 ::= 		 ()

tree ::= empty tree tex2html_wrap_inline118 (integer tree tree)

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


每個都是合法的表達式, 要注意負數情況, 以及 0 的情況(總和是 0 或者是 節點上的值是 0)

#include <stdio.h>
#define empty 0
int ans;
int solve(int sum, int target, char *c) {
int flag = 0, leaf = 0, token, neg;
while(*c == ' ' || *c == '\n')
*c = getchar();
if(*c == '(') {
token = 0, flag = 0, neg = 1;
while(*c = getchar()) {
if(*c >= '0' && *c <= '9')
token = token*10 + *c-'0', flag = 1;
else {
if(*c == '-') neg = -1;
else break;
}
}
token *= neg;
while(*c == ' ' || *c == '\n')
*c = getchar();
if(flag == 0) {
return empty;
}
int left = solve(sum+token, target, c);
while((*c = getchar()) != '(');
int right = solve(sum+token, target, c);
while((*c = getchar()) != ')');
if(left == 0 && right == 0) {
if(sum+token == target)
ans = 1;
}
return 1;
}
}
int main() {
int target;
char c;
while(scanf("%d", &target) == 1) {
ans = 0;
c = getchar();
solve(0, target, &c);
if(ans == 1)
puts("yes");
else
puts("no");
}
return 0;
}

台長: Morris
人氣(3,947) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][JAVA][BigNumber] 787 - Maximum Sub-sequence Product
此分類上一篇:[UVA] 1208 - Oreon

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文