1、UVa 11234 Expressions
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
using namespace std;
struct Node {
char data;
int left;
int right;
};
const int maxn = 10000 + 5;
char s[maxn];
stack<int> Stack;
Node node[maxn];
void build(void);
void bfs(int root);
int main()
{
// freopen("in.txt", "r", stdin);
int T;
cin>>T;
while (T--) {
cin>>s;
build();
bfs(Stack.top());
cout<<endl;
}
return 0;
}
void build(void)
{
int len = strlen(s);
for (int i=0; i<len; ++i) {
if (islower(s[i])) {
Stack.push(i);
node[i].data = s[i];
node[i].left = -1;
node[i].right = -1;
} else {
int right = Stack.top();
Stack.pop();
int left = Stack.top();
Stack.pop();
Stack.push(i);
node[i].data = s[i];
node[i].left = left;
node[i].right = right;
}
}
}
void bfs(int root)
{
Stack.pop();
queue<int> Queue;
stack<char> Stack2;
Stack2.push(node[root].data);
Queue.push(root);
while (!Queue.empty()) {
int n = Queue.front();
Queue.pop();
if (node[n].left != -1) {
int left = node[n].left;
int right = node[n].right;
Queue.push(left);
Queue.push(right);
Stack2.push(node[left].data);
Stack2.push(node[right].data);
}
}
while (!Stack2.empty()) {
cout<<Stack2.top();
Stack2.pop();
}
}
2、POJ 1241 Knockout Tournament
题意:
淘汰赛有 2^n 名选手,每名选手只要输一次就会被淘汰,没有被淘汰的选手继续进行淘汰赛
最后以完全二叉树的形式给出了淘汰赛的结果
问如果采用单循环赛制(所有参加比赛的选手均能相遇一次),选手可能到达的最高和最低排名
解题思路:
转自http://blog.csdn.net/ehi11/article/details/7831386
先按二叉树的顺序存图
然后分别计算所有的选手在淘汰赛中输了几个人和赢了几个人
最后可能到达的最高和最低排名就是 “肯定会输掉的场数+1” 和 “总数-肯定会赢的场数”
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5;
int Player[maxn];
int n;
struct Node {
int Lose;
int Win;
};
Node Tree[maxn];
int main()
{
// freopen("in.txt", "r", stdin);
while (scanf("%d", &n) != EOF && n != 0) {
int num = 1 << n;
for (int i=num; i<2*num; ++i) {
Player[i] = i-num+1;
}
int t = num;
for (int i=0; i<=n; ++i) {
t >>= 1;
for (int j=t; j<2*t; ++j) {
scanf("%d", &Player[j]);
}
}
memset(Tree, 0, sizeof(Tree));
for (int i=1; i<num; ++i) {
if (Player[i] != Player[i<<1]) {
Tree[Player[i<<1]].Lose = Tree[Player[i]].Lose + 1;
}
if (Player[i] != Player[i<<1|1]) {
Tree[Player[i<<1|1]].Lose = Tree[Player[i]].Lose + 1;
}
}
for (int i=num-1; i>=1; --i) {
Tree[Player[i]].Win = Tree[Player[i<<1]].Win + Tree[Player[i<<1|1]].Win + 1;
}
int m;
scanf("%d", &m);
while (m--) {
int k;
scanf("%d", &k);
printf("Player %d can be ranked as high as %d or as low as %d.\n", k, Tree[k].Lose+1, num-Tree[k].Win);
}
printf("\n");
}
return 0;
}
3、UVa 112 Tree Summing
#include <iostream>
#include <cstdio>
using namespace std;
int n;
int flag;
int judge_sum(int sum)
{
int data;
char c;
cin>>c;
cin>>data;
// cout<<cin<<endl;
if (cin == 0) {
cin.clear();
cin>>c;
return 0;
} else {
sum += data;
int a = judge_sum(sum);
int b = judge_sum(sum);
if (!a && !b && !flag) {
if (sum == n) {
flag = 1;
}
}
cin>>c;
return 1;
}
}
int main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while (cin>>n) {
flag = 0;
judge_sum(0);
cout<<(flag ? "yes" : "no")<<endl;
}
return 0;
}
4、HDU 3999 The order of a Tree
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 100000 + 5;
int root;
struct Node {
int left;
int right;
};
Node node[maxn];
void Insert(int root, int num);
void dfs(int root);
int main()
{
// freopen("in.txt", "r", stdin);
int n;
while (cin>>n) {
memset(node, -1, sizeof(node));
int num;
cin>>num;
root = num;
for (int i=1; i<n; ++i) {
cin>>num;
Insert(root, num);
}
cout<<root;
dfs(root);
cout<<endl;
}
return 0;
}
void Insert(int root, int num)
{
if (num < root) {
if (node[root].left == -1) {
node[root].left = num;
} else {
Insert(node[root].left, num);
}
} else {
if (node[root].right == -1) {
node[root].right = num;
} else {
Insert(node[root].right, num);
}
}
}
void dfs(int root)
{
if (node[root].left != -1) {
cout<<" "<<node[root].left;
dfs(node[root].left);
}
if (node[root].right != -1) {
cout<<" "<<node[root].right;
dfs(node[root].right);
}
}
5、UVa 122 Trees on the level
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
typedef long long ll;
typedef pair<int, int> Pair;
const int INF = 0x7fffffff;
const int maxn = 1e6;
int tree[maxn];
char str[500];
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
memset(tree, 0, sizeof(tree));
int max_node = 0;
bool flag = true;
while (scanf("%s", str) != EOF) {
if (strcmp(str, "()") == 0) {
for (int i = 2; i <= max_node; ++i) {
if (tree[i] && tree[i / 2] == 0) { flag = false; break; }
}
if (flag) {
for (int i = 1; i < max_node; ++i) {
if (tree[i]) { printf("%d ", tree[i]); };
}
printf("%d\n", tree[max_node]);
} else {
printf("not complete\n");
}
memset(tree, 0, sizeof(tree));
max_node = 0; flag = true;
} else {
int t = 1, num = 0, i;
for (i = 1; str[i] != ','; ++i) { num = num * 10 + str[i] - '0'; }
for (i = i + 1; str[i] != ')'; ++i) {
if (str[i] == 'L') { t *= 2; }
else { t = t * 2 + 1; }
}
max_node = max(max_node, t);
if (tree[t]) { flag = false; }
else { tree[t] = num; }
}
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
typedef long long ll;
typedef pair<int, int> Pair;
struct Node {
bool have_value;
int v;
Node *Left, *Right;
};
const int INF = 0x7fffffff;
const int maxn = 500;
Node* root;
char str[maxn];
bool flag = true;
int cnt = 0;
int ans[maxn];
bool read_input();
Node* new_node();
void add_node(int v, char* s);
bool bfs();
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
while (read_input()) {
if (bfs() && flag) {
REP(i, cnt - 1) { printf("%d ", ans[i]); }
printf("%d\n", ans[cnt - 1]);
} else {
printf("not complete\n");
}
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}
bool read_input() {
flag = true; cnt = 0;
root = new_node();
while (1) {
if (scanf("%s", str) != 1) { return false; }
if (strcmp(str, "()") == 0) { break; }
int v;
sscanf(&str[1], "%d", &v);
add_node(v, strchr(str, ',') + 1);
}
return true;
}
bool bfs() {
int front = 0, rear = 1;
Node* q[maxn];
q[0] = root;
while (front < rear) {
Node* u = q[front++];
if (!u->have_value) { return false; }
ans[cnt++] = u->v;
if (u->Left != NULL) { q[rear++] = u->Left; }
if (u->Right != NULL) { q[rear++] = u->Right; }
}
return true;
}
Node* new_node() {
Node* u = (Node*)malloc(sizeof(Node));
if (u != NULL) {
u->have_value = false;
u->Left = NULL; u->Right = NULL;
}
return u;
}
void add_node(int v, char* s) {
int n = strlen(s);
Node* u = root;
REP(i, n - 1) {
if (s[i] == 'L') {
if (u->Left == NULL) { u->Left = new_node(); }
u = u->Left;
} else {
if (u->Right == NULL) { u->Right = new_node(); }
u = u->Right;
}
}
if (u->have_value) { flag = false; }
u->v = v; u->have_value = true;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
typedef long long ll;
typedef pair<int, int> Pair;
const int INF = 0x7fffffff;
const int maxn = 500;
char str[maxn];
bool flag = true;
int root = 1, cnt = 0, ans_cnt = 0;
int Left[maxn], Right[maxn], val[maxn], ans[maxn];
bool read_input();
int new_node();
void add_node(int v, char* s);
bool bfs();
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
while (read_input()) {
if (bfs() && flag) {
REP(i, ans_cnt - 1) { printf("%d ", ans[i]); }
printf("%d\n", ans[ans_cnt - 1]);
} else {
printf("not complete\n");
}
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}
bool read_input() {
flag = true; ans_cnt = 0;
memset(val, 0, sizeof(val));
root = 1; Left[root] = 0; Right[root] = 0; cnt = root;
root = new_node();
while (1) {
if (scanf("%s", str) != 1) { return false; }
if (strcmp(str, "()") == 0) { break; }
int v;
sscanf(&str[1], "%d", &v);
add_node(v, strchr(str, ',') + 1);
}
return true;
}
bool bfs() {
int front = 0, rear = 1;
int q[maxn];
q[0] = root;
while (front < rear) {
int u = q[front++];
if (!val[u]) { return false; }
ans[ans_cnt++] = val[u];
if (Left[u] != 0) { q[rear++] = Left[u]; }
if (Right[u] != 0) { q[rear++] = Right[u]; }
}
return true;
}
int new_node() {
int u = ++cnt;
Left[u] = 0; Right[u] = 0;
return u;
}
void add_node(int v, char* s) {
int n = strlen(s);
int u = root;
REP(i, n - 1) {
if (s[i] == 'L') {
if (Left[u] == 0) { Left[u] = new_node(); }
u = Left[u];
} else {
if (Right[u] == 0) { Right[u] = new_node(); }
u = Right[u];
}
}
if (val[u]) { flag = false; }
val[u] = v;
}