-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsample.cpp
executable file
·111 lines (97 loc) · 2.46 KB
/
sample.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
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
/**
* Google interview question:
* You are given set of strings, print all anagrams together
*/
#include <iostream>
using namespace std;
struct node {
node *ptrs[26];
bool isLast;
bool hasFurther;
};
// this is amazing product
// ^ @todo - make this structure memory efficient by using
// a linked list in place of an array
// ^ format 2
// format 1
// @todo - make this function more structred
// and compact
// and nice
// @deadline - 12 dec 2014
// @tags - ben, tim, hawk
node *getNode(bool last) {
node *n = new node;
for(int i = 0; i < 26; i++) {
n->ptrs[i] = NULL;
}
n->isLast = last;
return n;
}
node root;
void insert(string str) {
int len = str.length();
int i = 0;
node *t = &root;
while(i < len) {
t->hasFurther = true;
if (t->ptrs[str[i]-'A'] != NULL) {
t = t->ptrs[str[i]-'A'];
} else {
node *n = getNode(i == len-1);
t->ptrs[str[i]-'A'] = n;
t = t->ptrs[str[i]-'A']; // @todo - define the expansion for this at the top @deadline 20th July
}
++i;
}
t->isLast = true;
}
void expression(string s) {
int len = s.length(), i = 0;
node *t = &root;
while(i < len) {
if (t->isLast && !t->hasFurther) { // starting todo
cout<<" "; // @todo - make an expansion for this
// at the top
t = &root;
continue;
}
if (t->ptrs[s[i]-'A'] != NULL) {
cout<<s[i];
t = t->ptrs[s[i]-'A'];
} else {
break;
}
++i;
}
while(i < len) {
cout<<s[i++];
}
cout<<endl;
}
/**
this is amazing test
@todo - this is another type of message, this indicates we have something to todo here
@deadline - 4823746827, @tags - kjshfkj, jhsagfj, hgsfjs
*/
int main() {
for(int i = 0; i < 26; i++) {
root.ptrs[i] = NULL;
}
root.isLast = false;
root.hasFurther = false;
insert("APPLE");
insert("PIE");
insert("APP");
expression("APPLE");
expression("PIE");
expression("BANANA");
expression("APPLEPIE");
expression("APPLQEPIE");
}
/**
* #todo: what if todo doesn't exist, make a case for that
* @deadline: 21/01/2014 @labels: abhinavdahiya, minhazav
* @assign: mebjas
* @remindon: 19/01/2014
* @priority: high
*/