-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp.cpp
82 lines (75 loc) · 1.71 KB
/
tmp.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <cstdlib>
using namespace std;
struct Node{
string name;
Node * left = NULL;
Node * right = NULL;
Node(string name):name(name){}
};
void find(Node* node, int& num){
if(node -> left) {
num++;
find(node -> left, num);
}
if(node -> right) {
num++;
find(node -> right, num);
}
}
int main(){
int m, n, count=0;
cin >> m >> n;
string name;
vector<Node*> nodes;
unordered_map<string, int> map;
while(cin >> name){
// if(name == "") break;
Node* node, *left, *right;
if(map.find(name) != map.end()){
node = nodes[map[name]];
}
else {
map[name] = count++;
node = new Node(name);
nodes.push_back(node);
}
cin >> name;
if(map.find(name) != map.end()){
left = nodes[map[name]];
}
else{
map[name] = count++;
left = new Node(name);
nodes.push_back(left);
}
node -> left = left;
cin >> name;
if(name != "*"){
if(map.find(name) != map.end()){
right = nodes[map[name]];
}
else{
map[name] = count++;
right = new Node(name);
nodes.push_back(right);
}
node -> right = right;
}
}
count = 0;
int num = 0;
vector<string> res;
for(auto node : nodes){
num = 0;
find(node, num);
if(num >= n) {
cout << node -> name << ' ';
res.push_back(node -> name);
}
}
return 0;
}