UVA_112_Tree Summing
2016-04-06 18:24
323 查看
#include<iostream> #include<sstream> #include<vector> #include<string> #include<stack> using std::cin; using std::cout; using std::endl; using std::vector; using std::string; using std::stringstream; using std::stack; const int MAX = 2147483647; typedef struct NodeType { int value; NodeType *pre = nullptr; NodeType *next = nullptr; bool flag = true; }NodeType, *Node; int result(Node p) { if (p) { return p->value + result(p->pre); } else { return 0; } } bool judge(stack<Node>&leaf, const int &sum) { while (!leaf.empty()) { auto p = leaf.top(); leaf.pop(); if (sum == result(p)) { return true; } } return false; } void scan(stack<Node>&leaf) { stack<char>bracket; stack<Node>value; char ch; int result = MAX; int sign = 1; input: while (cin >> ch) { if (ch == '-') { sign = -1; } else if (ch == '(' || ch == ')') { if (ch == '(') { if (!value.empty()) { if (result != MAX) { //value.emplace(nullptr,result,value.top(),true); auto p = new NodeType; p->value = result; p->pre = value.top(); p->next = nullptr; p->flag = true; value.top()->next = p; value.push(p); result = MAX; sign = 1; } } else { if (result != MAX) { auto p = new NodeType; p->value = result; p->pre = nullptr; p->next = nullptr; p->flag = true; value.push(p); result = MAX; sign = 1; } } bracket.push(ch); } else { bracket.pop(); if (bracket.empty() && value.empty()) { break; } if (value.top()->flag) { value.top()->flag = false; } else { if (!value.top()->next) { leaf.push(value.top()); } value.pop(); } } } //如果是数字 else { if (result == MAX) { result = 0; } result *= 10; if (sign > 0) { result += ch - '0'; } else { result -= ch - '0'; } } } } void print(stack<Node>&leaf, const int &sum) { if (judge(leaf, sum)) { cout << "yes" << endl; } else { cout << "no" << endl; } } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int sum; while (cin >> sum) { stack<Node> leaf; scan(leaf); print(leaf, sum); } return 0; }
相关文章推荐
- 机器学习:SVR支持向量机回归
- 课堂练习:返回一个整数数组中最大子数组的和
- 裸机初体验
- 发现可高速缓存的 SSL 页面
- HNACM(八)F-Distribution
- vs调试快捷键
- lsof 查看端口、程序以及开启文件的进程
- Java Socket 通信(同步阻塞式I/O)
- fork之后父子进程的文件描述符
- PHP JSON_ENCODE 不转义中文汉字的方法
- Java之趣味编程结婚问题
- 水骑士团队介绍
- unity 5.3 工业模型的细节展开功能实现
- Struts2 Action访问Servlet API
- Atitit..css的体系结构
- hibernate的增加、修改方法不生效,查询正常【事务没有拦截到导致的】
- Android-耗电量测试
- springMVC环境搭建
- opencv轮廓检测之椭圆检测-----算法篇(6)--ACTIVE CONTOURS
- 使用命令编译为jar包