UVA-112
Tree Summing
Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submit Status
Description
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 is the fundamental data structures in LISP, can easily is adapted to represent other important data structur Es such as trees.
This problem deals and determining whether binary trees represented as LISP s-expressions possess a certain property.
The problem
Given a binary tree of integers, you is to write a program that determines whether there exists a root-to-leaf path whose Nodes sum to a specified integer. For example, the tree shown below there is exactly four root-to-leaf paths. The sums of the paths are and 18.
Binary trees is represented in the input file as LISP s-expressions have the following form.
Empty Tree :: = ()Tree Empty Tree 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 is of the form (Integer () ())
Since an empty tree have no root-to-leaf paths, any query as to whether a path exists whose sum are a specified integer in a n 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-expressio N as described above. All binary tree s-expressions is valid, but expressions is spread over several lines and may contain spaces. There would be the one or more test cases in a input file, and input is terminated by End-of-file.
The Output
There should is one line of output for each test case (Integer/tree pair) in the input file. For each pair i,t (i represents the integer, and t represents the tree) the output is the String Yes if there is a root-to-leaf the path in T whose sum are I and no if there is no P Ath 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
Yesnoyesno
Source
Root:: Competitive programming 3:the New Lower Bound of programming contests (Steven & Felix Halim):: Graph:: Spec Ial 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
I'm a man! This problem really does me the whole person is not good ...
First the data is very pit!!
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 ()
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 () ())))
20 (5 () (5 () (5 () (5 () (4 () ()))))
Output:
Yes
No
Yes
No
No
Yes
Yes
Yes
Yes
Yes
No
No
No
No
There's a minus sign!!
There's a space between the minus sign and the number!!
I almost laughed out loud!!
hahaha haha%>_<%!!
AC Code:
#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; Determine if the leaf node gets (a), while (1) {for (int i=0; a[i]! = '; 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 && s Um = = N) flag = 1; Before the direct break here, resulting in no input into the back, tangled me for a long time sum-= Snum.top (); Snum.pop (); iswei = 0;} Sch.pop ();//printf ("%d\n", Sum);}} if (Sch.empty ()) break; When all () is matched and the stack is empty, the tree output is complete., and then jumps out of the loop else {gets (a);}} if (flag = = 1) printf ("yes\n"), Else printf ("no\n");} return 0;}
Also attached to the Code of Daniel (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 (sum of the numbers!) The application of the stack! )