-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheritage.cpp
executable file
·53 lines (48 loc) · 1.08 KB
/
heritage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
ID: paulius10
PROG: heritage
LANG: C++
*/
#include <fstream>
#include <map>
using namespace std;
void initialize(map<char, bool> m) {
for (char c = 'A'; c <= 'Z'; c++) m[c] = false;
}
void print(string in, string pre, map<char, bool> considering, ofstream &fout) {
char root = '0';
for (int i = 0; i < pre.size(); i++) {
if (considering[pre[i]]) {
root = pre[i];
break;
}
}
if (root == '0') return;
map<char, bool> left;
initialize(left);
int i;
for (i = 0; in[i] != root; i++) {
if (considering[in[i]]) left[in[i]] = true;
}
map<char, bool> right;
for (i++; i < in.size(); i++) {
if (considering[in[i]]) right[in[i]] = true;
}
print(in, pre, left, fout);
print(in, pre, right, fout);
fout << root;
}
int main() {
ifstream fin("heritage.in");
string in, pre;
fin >> in >> pre;
fin.close();
map<char, bool> everything;
initialize(everything);
for (int i = 0; i < in.length(); i++) everything[in[i]] = true;
ofstream fout("heritage.out");
print(in, pre, everything, fout);
fout << endl;
fout.close();
return 0;
}