-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtries_contact.js
128 lines (89 loc) · 2.47 KB
/
tries_contact.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var Node = function(){
this.children= [];
this.last_char=false;
this.cont=0;
};
function add_(word, node=this.root){
char_pos=word.charCodeAt(0) - 'a'.charCodeAt(0);
if(node.last_char==true && word.length>1){
node.cont++;
};
if(word.length==1){
if(word[0]=="a"){
node.children[0]=new Array("a", new Node());
node.last_char=true;
node.cont++;
return;
};
if(node.children[char_pos]==null){
node.children[char_pos]=new Array(word[0], new Node());
node.last_char=true;
node.cont++;
return;
}else{
if(node.children[char_pos][0]==word[0]){
node.last_char=true;
node.cont++;
};
return;
};
}else{
if(word[0]=="a"){
node.children[0]=new Array("a", new Node());
}else{
if(node.children[char_pos]==null){
node.children[char_pos]=new Array(word[0], new Node());
};
};
};
word=word.substring(1);
add_(word, node.children[char_pos][1]);
};
function find_(word, node=this.root){
//console.log("word: " + word[0]);
if(word.length==0 && node.last_char==false) return node.cont;
char_pos=word.charCodeAt(0) - 'a'.charCodeAt(0);
if(node.children[char_pos]=='undefined' || node.children[char_pos]=='null' || node.children[char_pos]=== undefined || node.children[char_pos]=== null || node.children[char_pos]==='undefined'){
var cont=0;
return cont;
};
if(word[0]==node.children[char_pos][0]){
word=word.substring(1);
if(node.last_char==true && word.length==0){
return node.cont;
}else{
return find_(word, node.children[char_pos][1]);
};
};
};
main();
function main(){
do{
var n=window.prompt("Insert the number of operations to perform"); //casos de prueba
}while(n>Math.pow(10,5));
var i=1;
node = this.root=new Node(); //declarando el nodo raiz
while(i<=n){
do{
var word='';
word=window.prompt("Insert the operation "+ i + " to perform");
word=word.toLowerCase();
if(word[0]==='a'){
var oper=word.substring(0,3);
word=word.substring(4);
}else{
if(word[0]==='f'){
var oper=word.substring(0,4);
word=word.substring(5);
};
};
}while(word.length>21 && (!(oper==='find' || oper==='add')));
if(oper==='add'){
add_(word, this.node);
}else{
var cant_pal=find_(word, this.node);
console.log(cant_pal);
};
i++;
};
};