-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrachi.cpp
More file actions
110 lines (86 loc) · 1.9 KB
/
Copy pathPrachi.cpp
File metadata and controls
110 lines (86 loc) · 1.9 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<iostream>
#include<vector>
#include<map>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <istream>
#include<string.h>
#include<stdio.h>
using namespace std;
struct Node
{
string name;
bool visited;
vector<Node*> childs;
};
void ProcessLine(string str1,map<string,Node*> &mp)
{
char* str = (char*)str1.c_str();
char* parent = strtok(str,":");
int len = strlen(parent);
if(parent[len-1] == ' ')
parent[len-1] = '\0';
if(mp[parent] == NULL)
{
Node* node = new Node();
node->name = parent;
node->visited = false;
node->childs.clear();
mp[parent] = node;
}
char* temp = strtok(NULL,",");
while(temp != NULL)
{
if(*temp == ' ')
++temp;
Node *obj = mp[temp];
if(obj != NULL)
{
mp[parent]->childs.push_back(obj);
}
else
{
Node* node = new Node();
node->name = temp;
node->visited = false;
node->childs.clear();
mp[temp] = node;
mp[parent]->childs.push_back(node);
}
temp = strtok(NULL,",");
}
}
void DFSUtil(Node* root)
{
if(root != NULL && root->visited == false)
{
root->visited = true;
vector<Node*>::iterator it = root->childs.begin();
for(it = root->childs.begin(); it != root->childs.end(); ++it)
{
DFSUtil(*it);
}
}
}
int main()
{
string line;
map<string,Node*> mp;
while(getline(cin,line))
{
ProcessLine(line,mp);
}
DFSUtil(mp["Nick Fury"]);
map<string,Node*>::iterator it = mp.begin();
for(it = mp.begin(); it != mp.end(); ++it)
{
Node* node = it->second;
if(node != NULL && node->visited == false)
{
cout<<node->name<<", ";
}
}
return 0;
}