现在的位置: 首页 > 综合 > 正文

UVa 112 Tree Summing

2012年12月14日 ⁄ 综合 ⁄ 共 2851字 ⁄ 字号 评论关闭

UVa 112 Tree Summing

Table of Contents

1 题目

===================


 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, Trepresents 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

===================

2 思路

用一个数组记录下从根到当前位置所有的数字,每次到达叶子结点时,计算一下当前的 总和是否与需要的数字相等,每次返回上一层时,从sum中减去当前的数字。关键在于 弄清楚满足什么条件时,有新数字产生,满足什么条件时,到达了叶子结点,满足什么 条件时,需要返回上一级。另外,输入数字可能是负数,负号与数字之间可能有空格, 随时可能有换行,等等细节都需要考虑。

3 代码

/*
 * Problem: UVa 112 Tree Summing
 * Lang: ANSI C
 * Time: 0.035s
 * Author: minix
 */
#include <stdio.h>
#include <ctype.h>
#define N 10000
int s[N];
int main() {
  int t, l, p, sum, y, r, f;
  char c, pc;
  while (scanf("%d",&t) != EOF) {
    sum = 0; y = 0; r = 0;
    while ((c=getchar()) != '(');
    l = 1; p = 0; pc = '('; f = 1;
    while (l!=0) {
      if (c=='(' || c==')')
        pc = c;
      c = getchar();
      if (c==' '||c=='\n') continue;
      if (c=='-') { f *= -1; continue; }

      if (c=='(') { 
        if (pc=='(') { /* get a new number */
          r = 0;
          p = f * p;
          sum = sum + p; 
          s[l] = p; 
          f = 1;
          p=0; 
        }
        l++;
      }
      if (c==')') { 
        if (pc=='(') r++;
        if (pc==')') { sum -= s[l]; s[l] = 0;} /* back to upper layer */
        l--; 
        if (r==2 && pc=='(') { /* encounter a leaf */
          if (sum == t) {
            y = 1;
            break;
          }
        }
      }
      if (isdigit(c)) { 
        p = 10*p+c-'0'; 
      }
    }
    while (l!=0) {
      c = getchar();
      if (c=='(') l++;
      if (c==')') l--;
    }
    c = getchar();
    if (y) puts("yes");
    else puts("no");
  }

  return 0;
}

抱歉!评论已关闭.