-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnumeros_de_telephone.cpp
65 lines (56 loc) · 1.03 KB
/
numeros_de_telephone.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
// Read inputs from stdin. Write outputs to stdout.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Node
{
public:
Node(char c)
{
val = c;
}
char val;
vector<Node*> children;
};
void add_tel(vector<Node*>* roots, const string& val)
{
Node* node = NULL;
char c = val.at(0);
for(int i=0; i<roots->size(); i++) {
if((*roots)[i]->val == c) {
node = (*roots)[i];
break;
}
}
if(node == NULL) {
node = new Node(c);
roots->push_back(node);
}
if(val.length() > 1)
return add_tel(&node->children, val.substr(1));
}
int count_nodes(const vector<Node*>& roots)
{
int count = 0;
for(int i=0; i<roots.size(); i++) {
count += 1 + count_nodes(roots[i]->children);
}
return count;
}
int main()
{
vector<Node*> roots;
int n;
cin >> n;
for(int i = 0; i < n; i++) {
string tel;
getline(cin, tel);
if(tel == "")
getline(cin, tel);
add_tel(&roots, tel);
}
int count = count_nodes(roots);
cout << count << endl;
return 0;
}