Tree Summing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
#include <iostream> #include <cstdio> #include <sstream> #include <string> using namespace std; int I; string str; int next_index; bool PATHFOUND; bool isEmpty(int a,int b) { if(a+1==b && str[a]=='(' && str[b]==')') return 1; return 0; } int findnum(int idx) { int numfoundAt=-1; int num=0; bool negative=false; for(int i=idx;i<str.size();i++) { if(str[i]!='(' && str[i]!=')') { numfoundAt = i; if(str[i]=='-') negative = 1, numfoundAt+=1; break; } } while( numfoundAt<str.size() && (str[numfoundAt]-'0')>=0 && (str[numfoundAt]-'0')<=9) { num*=10; num+=(str[numfoundAt]-'0'); numfoundAt+=1; } if(negative) num*=(-1); next_index = numfoundAt; return num; } //(5 (4 (11 (7()()) (2()()) ) () ) void calc(int a,int b,int sum) { int number = findnum(a); int j=next_index; int start1,start2,finish1,finish2; int left=0,right=0; while(j<str.size() && str[j]!='(') j+=1; left=1; start1=j; j+=1; while(left!=right) { if(str[j]=='(') left+=1; if(str[j]==')') right+=1; j+=1; } finish1=j-1; left=1;right=0; start2=j; j+=1; while(left!=right) { if(str[j]=='(') left+=1; if(str[j]==')') right+=1; j+=1; } finish2=j-1; bool f1 = isEmpty(start1,finish1); bool f2 = isEmpty(start2,finish2); if(f1==1 && f2==1) { if(sum+number==I) { PATHFOUND=1; return; } } else if(f1) { calc(start2,finish2,sum+number); } else if(f2) { calc(start1,finish1,sum+number); } else { calc(start1,finish1,sum+number); calc(start2,finish2,sum+number); } } //22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()())))) int main() { char ch; int j,start1,start2,finish1,finish2; bool foundNum,neg; int left,right,num,counter; while(scanf("%d",&I)!=EOF) { str=""; while(scanf("%c",&ch)) if(ch=='(') break; str+='('; left=1;right=0; while(left!=right) { scanf("%c",&ch); if(ch!=' ' && ch!='\n') str+=ch; if(ch=='(') left+=1; if(ch==')') right+=1; } //cout<<str<<"\n"; if(str[0]=='(' && str[1]==')') cout<<"no\n"; else { PATHFOUND=0; int number = findnum(0); //left tree j=next_index; left=0;right=0; while(j<str.size() && str[j]!='(') j+=1; left=1; start1=j; j+=1; while(left!=right) { if(str[j]=='(') left+=1; if(str[j]==')') right+=1; j+=1; } finish1=j-1; left=1;right=0; start2=j; j+=1; while(left!=right) { if(str[j]=='(') left+=1; if(str[j]==')') right+=1; j+=1; } finish2=j-1; bool f1 = isEmpty(start1,finish1); bool f2 = isEmpty(start2,finish2); //cout<<f1<<" "<<f2<<"\n"; //cout<<start1<<" "<<finish1<<" "<<start2<<" "<<finish2<<"\n"; if(f1==1 && f2==1) { if(number==I) PATHFOUND=1; else PATHFOUND=0; } else if(f1) { calc(start2,finish2,number); } else if(f2) { calc(start1,finish1,number); } else { calc(start1,finish1,number); calc(start2,finish2,number); } if(PATHFOUND) cout<<"yes\n"; else cout<<"no\n"; } } return 0; } |