uva112 - Tree Summing

题目大意是首先给出一个N , 然后通过读入 括号和数字 建立一棵树, 算出是否有一条路径值和是N 。 有输出yes,否是no

 

做的时候输入有点蛋疼,。 借鉴了网上了他人的代码,非常简洁。。学习了下。。 原地址:http://www.cnblogs.com/xiaocai905767378/archive/2011/11/02/2232750.html

 

题目:

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


代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 
 6 bool ok=false;
 7 
 8 bool solve(int n,int final)
 9 {
10     int v;char c;
11     cin>>c;
12     if(!(cin>>v)==0)
13     {
14         n+=v;
15         bool t =solve(n,final) | solve(n,final);
16 
17         if(!ok && !t)ok=(n==final);
18         cin>>c;
19         return true;
20     }
21     else  //上一个碰到了')'或者不合法字符
22     {
23         cin.clear();
24         cin>>c;    //读掉')'或者不合法字符
25         return false;
26     }
27 
28 }
29 int main()
30 {
31     int final;
32 
33   //  freopen("in","r",stdin);
34 
35     while(scanf("%d",&final)!=EOF)
36     {
37         ok=false;
38         solve(0,final);
39         if(ok)cout<<"yes"<<endl;
40         else cout<<"no"<<endl;
41     }
42 
43     return 0;
44 }

 

posted @ 2013-11-26 21:14  doubleshik  阅读(152)  评论(0编辑  收藏  举报