/* 題目: UVa 112 - Tree Summing
* Language: C++
* Created on: 2016年4月7日
* Author: hanting
*/
#include <iostream>
#include <cstring> // memset
#include <sstream>
using namespace std;
const int maxN = 100000;
int arr[maxN];
bool arr2[maxN];
string tree;
stringstream sin;
bool check;
void buildTree(int ptr)
{
if(ptr < maxN)
{
char ch;
sin >> ch;
sin >> ch;
if(ch == '*')
{
int num;
sin >> num;
arr[ptr] = num;
arr2[ptr] = true;
buildTree(ptr*2);
buildTree(ptr*2+1);
sin >> ch;
}
/*else if(ch == ')')
{
}*/
}
}
void init()
{
memset(arr, 0, sizeof(arr));
memset(arr2, 0, sizeof(arr2));
tree.clear();
check = false;
int flag = 0;
char c;
char last;
while(c = cin.get())
{
if(tree.size()) last = tree[tree.size()-1];
if(c == ' ' or c == '\n') continue;
if(c == '(') flag++;
else if(c == ')') flag--;
else if(isdigit(c) and (last != '-' and !isdigit(last)))
{
tree += '*';
}
else if(c == '-')
{
tree += '*';
}
if(flag >= 0) tree += c;
if(flag == 0) break;
}
sin.clear();
sin.str(tree);
}
void DFS(int index, int n)
{
if(index < maxN and arr2[index])
{
n -= arr[index];
if(!arr2[index*2] and !arr2[index*2+1] and n == 0)
{
check = true;
return ;
}
DFS(index*2, n);
DFS(index*2+1, n);
}
}
int main()
{
int n;
int i = 1;
while(cin >> n)
{
init();
buildTree(1);
if(arr2[1])DFS(1, n);
cout << (check ? "yes\n":"no\n");
}
return 0;
}
2016年4月7日 星期四
[UVA] 112 - Tree Summing
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言