Skip to content

Commit 090236d

Browse files
committed
Solve 1133, 1191m 1194, 1200, 1550, 1552, 1764 and 1774 from URI
1 parent 72991eb commit 090236d

9 files changed

+531
-0
lines changed

URI/1133 - Rest of a Division.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
int main() {
7+
long long x, y;
8+
cin >>x >> y;
9+
10+
long long n = min(x, y);
11+
long long m = max(x, y);
12+
for(int i = n + 1; i < m; i++) {
13+
if(i % 5 == 2 || i % 5 == 3)
14+
cout<<i<<endl;
15+
}
16+
17+
return 0;
18+
}

URI/1191 - Tree Recovery.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <iostream>
2+
#include <memory.h>
3+
4+
using namespace std;
5+
6+
struct node {
7+
node *left, *right;
8+
char value;
9+
static node *sentinel;
10+
11+
node() {
12+
memset(this, 0, sizeof *this);
13+
}
14+
15+
node(char key) {
16+
left = right = sentinel;
17+
this->value = key;
18+
}
19+
};
20+
21+
node *node::sentinel = new node();
22+
23+
void free_bst(node *cur) {
24+
if(cur == node::sentinel)
25+
return;
26+
27+
free_bst(cur->left);
28+
free_bst(cur->right);
29+
30+
delete cur;
31+
}
32+
33+
void post_order(node *cur) {
34+
if(cur == node::sentinel)
35+
return;
36+
37+
post_order(cur->left);
38+
post_order(cur->right);
39+
cout << cur->value;
40+
}
41+
42+
string preord, inord;
43+
node *root;
44+
45+
void build_bst(node *&cur, int l, int r) {
46+
if(l > r)
47+
return;
48+
49+
cur = new node(preord[0]);
50+
51+
int cur_in = inord.find(preord[0]);
52+
preord.erase(0, 1);
53+
54+
build_bst(cur->left, l, cur_in - 1);
55+
build_bst(cur->right, cur_in + 1, r);
56+
}
57+
58+
int main() {
59+
while(cin >> preord >> inord) {
60+
root = NULL;
61+
62+
build_bst(root, 0, preord.length() - 1);
63+
post_order(root);
64+
cout << endl;
65+
66+
free_bst(root);
67+
}
68+
69+
return 0;
70+
}
71+

URI/1194 - Pre, In and Post.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <stdio.h>
2+
#include <iostream>
3+
#include <memory.h>
4+
5+
using namespace std;
6+
7+
struct node {
8+
node *left, *right;
9+
char value;
10+
static node *sentinel;
11+
12+
node() {
13+
memset(this, 0, sizeof *this);
14+
}
15+
16+
node(char key) {
17+
left = right = sentinel;
18+
this->value = key;
19+
}
20+
};
21+
22+
node *node::sentinel = new node();
23+
24+
void free_bst(node *cur) {
25+
if(cur == node::sentinel)
26+
return;
27+
28+
free_bst(cur->left);
29+
free_bst(cur->right);
30+
31+
delete cur;
32+
}
33+
34+
void post_order(node *cur) {
35+
if(cur == node::sentinel)
36+
return;
37+
38+
post_order(cur->left);
39+
post_order(cur->right);
40+
cout << cur->value;
41+
}
42+
43+
string preord, inord;
44+
node *root;
45+
46+
void build_bst(node *&cur, int l, int r) {
47+
if(l > r)
48+
return;
49+
50+
cur = new node(preord[0]);
51+
52+
int cur_in = inord.find(preord[0]);
53+
preord.erase(0, 1);
54+
55+
build_bst(cur->left, l, cur_in - 1);
56+
build_bst(cur->right, cur_in + 1, r);
57+
}
58+
59+
int main() {
60+
int t, n;
61+
cin >> t;
62+
while(t-- != 0) {
63+
cin >> n >> preord >> inord;
64+
root = NULL;
65+
66+
build_bst(root, 0, preord.length() - 1);
67+
post_order(root);
68+
cout << endl;
69+
70+
free_bst(root);
71+
}
72+
73+
return 0;
74+
}
75+

URI/1200 - BST Operations I.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include <memory.h>
5+
6+
using namespace std;
7+
8+
struct node {
9+
node *left, *right;
10+
char value;
11+
static node *sentinel;
12+
13+
node() {
14+
memset(this, 0, sizeof *this);
15+
}
16+
17+
node(char value) {
18+
left = right = sentinel;
19+
this->value = value;
20+
}
21+
};
22+
23+
node *node::sentinel = new node;
24+
25+
vector<char> trav;
26+
27+
node * insert(node *cur, char &value) {
28+
if(cur == node::sentinel) return new node(value);
29+
if(cur->value == value) return cur;
30+
cur->value > value ? cur->left = insert(cur->left, value) : cur->right = insert(cur->right, value);
31+
return cur;
32+
}
33+
34+
bool search(node *cur, char value) {
35+
if(cur == node::sentinel) return false;
36+
if(cur->value == value) return true;
37+
return cur->value > value ? search(cur->left, value) : search(cur->right, value);
38+
}
39+
40+
void pre_order(node *cur) {
41+
if(cur == node::sentinel) return;
42+
trav.push_back(cur->value);
43+
pre_order(cur->left);
44+
pre_order(cur->right);
45+
}
46+
47+
void in_order(node *cur) {
48+
if(cur == node::sentinel) return;
49+
in_order(cur->left);
50+
trav.push_back(cur->value);
51+
in_order(cur->right);
52+
}
53+
54+
void post_order(node *cur) {
55+
if(cur == node::sentinel) return;
56+
post_order(cur->left);
57+
post_order(cur->right);
58+
trav.push_back(cur->value);
59+
}
60+
61+
void print() {
62+
for(int i = 0; i < (int)trav.size(); ++i)
63+
cout << (i ? " " : "") << trav[i];
64+
cout << endl;
65+
}
66+
67+
void free_bst(node *cur) {
68+
if(cur == node::sentinel)
69+
return;
70+
71+
free_bst(cur->left);
72+
free_bst(cur->right);
73+
74+
delete cur;
75+
}
76+
77+
int main() {
78+
node *root = NULL;
79+
80+
string s;
81+
char c;
82+
while(cin >> s) {
83+
trav.clear();
84+
85+
if(s == "PREFIXA") {
86+
pre_order(root);
87+
print();
88+
} else if(s == "INFIXA") {
89+
in_order(root);
90+
print();
91+
} else if(s == "POSFIXA") {
92+
post_order(root);
93+
print();
94+
} else if(s == "I") {
95+
cin >> c;
96+
if(root == NULL)
97+
root = new node(c);
98+
else
99+
insert(root, c);
100+
} else {
101+
cin >> c;
102+
if(search(root, c))
103+
cout << c << " existe" << endl;
104+
else
105+
cout << c << " nao existe" << endl;
106+
}
107+
}
108+
109+
free_bst(root);
110+
111+
return 0;
112+
}
113+

URI/1550 - Inversion.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <stdio.h>
2+
#include <memory.h>
3+
#include <queue>
4+
5+
using namespace std;
6+
7+
int from, to;
8+
bool vis[1000001];
9+
queue<int> qu;
10+
11+
int reverse(int x) {
12+
int tmp = 0;
13+
while(x != 0) {
14+
tmp *= 10;
15+
tmp += x % 10;
16+
x /= 10;
17+
}
18+
return tmp;
19+
}
20+
21+
int BFS() {
22+
while(!qu.empty()) qu.pop();
23+
qu.push(from);
24+
25+
int size, fr, cost = 0;
26+
while(!qu.empty()) {
27+
size = qu.size();
28+
29+
while(size--) {
30+
fr = qu.front();
31+
qu.pop();
32+
33+
if(fr == to)
34+
return cost;
35+
36+
if(!vis[fr + 1])
37+
qu.push(fr + 1);
38+
vis[fr + 1] = true;
39+
40+
fr = reverse(fr);
41+
if(!vis[fr])
42+
qu.push(fr);
43+
vis[fr] = true;
44+
}
45+
46+
++cost;
47+
}
48+
49+
return cost;
50+
}
51+
52+
int main() {
53+
int t;
54+
scanf("%d", &t);
55+
while(t-- != 0 ) {
56+
scanf("%d %d", &from, &to);
57+
memset(vis, false, sizeof vis);
58+
printf("%d\n", BFS());
59+
}
60+
61+
return 0;
62+
}
63+

0 commit comments

Comments
 (0)