题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=48
注意:1、有负数;2、空树答案为no.
版本一:
#include <iostream>
using namespace std;
bool proc_tree(int sum, int des, bool &chld)
{
char c;
int t;
bool lchld, rchld;
cin >> c;
if(cin >> t)
{
sum += t;
chld = true;
bool flag = proc_tree(sum, des, lchld) | proc_tree(sum, des, rchld); //此处若用||,可能会因为第一个操作数为真而不执行第二个操作数。
cin >> c;
if(!lchld && !rchld) return sum == des; //如果是叶子节点。
else return flag;
}
else
{
chld = false;
cin.clear();
cin >> c;
return false;
}
}
int main()
{
int des;
while(cin >> des)
{
bool chld;
cout << (proc_tree(0, des, chld) ? "yes" : "no") << endl;
}
return 0;
}
版本二:递归建树 + 搜索
#include <iostream>
using namespace std;
struct Node
{
int val;
int left, right;
Node(): left(-1), right(-1) {}
};
int cnt, des;
Node tree[10000];
int buildTree()
{
char c;
int t;
cin >> c;
if(cin >> t)
{
int tmp = ++cnt;
tree[tmp].val = t;
tree[tmp].left = buildTree();
tree[tmp].right = buildTree();
cin >> c;
return tmp;
}
cin.clear();
cin >> c;
return -1;
}
bool dfs(int idx, int sum)
{
if(idx == -1) return false;
sum += tree[idx].val;
if(tree[idx].left == -1 && tree[idx].right == -1)
{
return sum == des;
}
return dfs(tree[idx].left, sum) || dfs(tree[idx].right, sum);
}
void reset()
{
for(int i = 0; i <= cnt; ++ i)
{
tree[i].left = -1;
tree[i].right = -1;
}
}
int main()
{
while(cin >> des)
{
cnt = -1;
int head = buildTree();
cout << (dfs(head, 0) ? "yes" : "no") << endl;
reset();
}
return 0;
}
posted @
2011-11-24 21:50 wuxu 阅读(536) |
评论 (0) |
编辑 收藏
posted @
2011-11-24 15:38 wuxu 阅读(862) |
评论 (0) |
编辑 收藏
posted @
2011-11-23 16:16 wuxu 阅读(256) |
评论 (0) |
编辑 收藏
posted @
2011-11-23 13:27 wuxu 阅读(528) |
评论 (0) |
编辑 收藏
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=1093
首先找到需要移动的字符串,方法如下:以初始序列为准,设初始序列下标为i, 目的序列下标为j, 从n-1开始,如果两下标对应的字符串相等,下标同时减一,否则仅初始序列下标减一。那么目的序列中还未被成功匹配的字符串就是需要移动的字符串。要使移动次数最少,显然应该按未被处理的目的序列中字符串逆序移动(输出)。
#include <iostream>
#include <string>
using namespace std;
int k, n;
string org[210], des[210];
int main()
{
int i, j;
cin >> k;
while(k--)
{
cin >> n;
cin.ignore();
for(i = 0; i < n; ++i)
getline(cin, org[i]);
for(i = 0; i < n; ++i)
getline(cin, des[i]);
for(i = j = n - 1; i >= 0; --i)
{
if(org[i] == des[j]) --j;
}
for( ; j >= 0; --j)
cout << des[j] << endl;
cout << endl;
}
return 0;
}
posted @
2011-11-22 16:36 wuxu 阅读(851) |
评论 (0) |
编辑 收藏
posted @
2011-11-19 22:12 wuxu 阅读(261) |
评论 (0) |
编辑 收藏
题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=37
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>
#include <string>
#include <algorithm>
#include <cstddef>
using namespace std;
int n;
vector<int> block[30];
void move_onto(int ia, int a, int ib, int b)
{
int tmp;
tmp = block[ia].back();
while(tmp != a)
{
block[ia].pop_back();
block[tmp].push_back(tmp);
tmp = block[ia].back();
}
tmp = block[ib].back();
while(tmp != b)
{
block[ib].pop_back();
block[tmp].push_back(tmp);
tmp = block[ib].back();
}
block[ia].pop_back();
block[ib].push_back(a);
}
void move_over(int ia, int a, int ib, int b)
{
int tmp;
tmp = block[ia].back();
while(tmp != a)
{
block[ia].pop_back();
block[tmp].push_back(tmp);
tmp = block[ia].back();
}
block[ia].pop_back();
block[ib].push_back(a);
}
void pile_onto(int ia, int a, int ib, int b)
{
int tmp;
tmp = block[ib].back();
while(tmp != b)
{
block[ib].pop_back();
block[tmp].push_back(tmp);
tmp = block[ib].back();
}
vector<int>::iterator it;
it = find(block[ia].begin(), block[ia].end(), a);
block[ib].insert(block[ib].end(), it, block[ia].end());
block[ia].erase(it, block[ia].end());
}
void pile_over(int ia, int a, int ib, int b)
{
vector<int>::iterator it;
it = find(block[ia].begin(), block[ia].end(), a);
block[ib].insert(block[ib].end(), it, block[ia].end());
block[ia].erase(it, block[ia].end());
}
void manipulate(const string &s)
{
string s1, s2;
int a, b, ia, ib;
stringstream str(s);
str >> s1 >> a >> s2 >> b;
if(a == b) return;
for(int i = 0; i < n; ++i)
{
for(size_t j = 0; j != block[i].size(); ++j)
{
if(block[i][j] == a) ia = i;
if(block[i][j] == b) ib = i;
}
}
if(ia == ib) return;
if(s1 == "move" && s2 == "onto") move_onto(ia, a, ib, b);
else if(s1 == "move" && s2 == "over") move_over(ia, a, ib, b);
else if(s1 == "pile" && s2 == "onto") pile_onto(ia, a, ib, b);
else pile_over(ia, a, ib, b);
}
int main()
{
string s;
cin >> n;
for(int i = 0; i < n; ++i)
block[i].push_back(i);
cin.ignore();
while(getline(cin, s) && s != "quit")
manipulate(s);
for(int i = 0; i < n; ++i)
{
cout << i << ':';
for(size_t j = 0; j != block[i].size(); ++j)
cout << ' ' << block[i][j];
cout << endl;
}
return 0;
}
posted @
2011-11-19 00:04 wuxu 阅读(327) |
评论 (0) |
编辑 收藏
posted @
2011-11-15 00:03 wuxu 阅读(495) |
评论 (0) |
编辑 收藏
posted @
2011-10-21 15:47 wuxu 阅读(265) |
评论 (0) |
编辑 收藏
摘要: 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=98&problem=1135&mosmsg=Submission+received+with+ID+9393629
Code highlighting ...
阅读全文
posted @
2011-10-21 15:27 wuxu 阅读(244) |
评论 (0) |
编辑 收藏