Background
Lisp was one of the earliest high-level programming languages and, with Fortran, is one of the oldest syntax ages 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.
Binary Trees are represented in the input file as lisp s-expressions having the following form.
empty tree ::= ()
Tree: =Empty tree(IntegerTree Tree)
The tree diagrammed above is represented by the expression (5 (4 (11 () (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 shoshould be one line of output for each test case (integer/tree pair) in the input file. For each pairI,T(IRepresents the integer,TRepresents the tree) the output is the stringYesIf there is a root-to-leaf path inTWhose sum isIAndNoIf there is no path inTWhose sum isI.
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
# Include <iostream> # include <stack> # include <cstring> # include <cstdio> # include <string> # include <algorithm> # include <queue> # include <set> # include <map> # include <fstream> # include <stack> # include <list> # include <sstream> using namespace STD; # define MS (ARR, Val) memset (ARR, Val, sizeof (ARR )) # define n 1000000 # define INF 0x3fffffff # define Vint vector <int> # define setint set <int> # define mint Map <int, int> # Define lint list <int> # define Sch stack <char> # define qch queue <char> # define Sint stack <int> # define qint queue <int> struct node {int val; int L; int R; int tag; // build flag void set (INT Val) {L = r = 0; tag = 2; this-> val = val ;}} tree [N]; char seq [N]; int Val, root, P, I; Sint Si; int t; bool ldigit () // the right side of the left bracket is a number, and move it to the next left bracket. If it exists, {T ++; If (isdigit (SEQ [T]) // positive number {val = seq [T]-'0 '; while (isdigit (SEQ [++ T]) // Obtain the number {val = Val * 10 + seq [T]-'0';} return true;} If (SEQ [T] = '-') // negative {val = 0; while (isdigit (SEQ [++ T]) // obtain the number {val = Val * 10 + seq [T]-'0';} val =-val; return true ;} while (SEQ [++ T] & seq [T]! = '('); Return false;} void getseq () // read the input. Note that a negative number still exists. {int con = 1, n = 0 was not taken into account at the beginning; char ch; while (CH = getchar (), ch! = '('); Seq [n ++] = '('; while (CON) {CH = getchar (); If (CH = '(') {con ++; seq [n ++] = CH;} else if (CH = ') {con --; seq [n ++] = CH ;} else if (isdigit (CH) {seq [n ++] = CH;} else if (CH = '-') {seq [n ++] = CH ;}} seq [N] = '\ 0'; t = 0;} void build () // build {root = 0; if (ldigit () {tree [++ root]. set (VAL); SI. push (Root);} int POs, TMP; while (! Si. empty () {If (ldigit () {tree [++ root]. set (VAL); Pos = root;} else Pos = 0; TMP = SI. top (); If (tree [TMP]. tag = 2) // left subtree {tree [TMP]. L = Pos; tree [TMP]. tag --;} else // right subtree {tree [TMP]. R = Pos; SI. pop ();} If (POS) {Si. push (POS) ;}} bool DFS (int I) {bool tag = false; Val + = tree [I]. val; If (tree [I]. l | tree [I]. r) {If (tree [I]. L & DFS (tree [I]. l) {return true;} If (tree [I]. R & DFS (tree [I]. R) {return true ;}} else {tag = I = Val? True: false;} Val-= tree [I]. val; // return tag with the wrong position;} int main () {// freopen ("in. in "," r ", stdin); While (~ Scanf ("% d", & I) {getseq (); Build (); val = 0; If (root & DFS (1 )) puts ("yes"); else puts ("no");} return 0 ;}