Skip to content

Commit 06b822e

Browse files
committed
Done algorithm of 200UVaOJ
Still not finished. The algorithm is implemented, but it needs to read multiple alphabets.
1 parent 9898c21 commit 06b822e

File tree

6 files changed

+277
-0
lines changed

6 files changed

+277
-0
lines changed

uDebug/200UVaOJ/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
g++ -std=c++11 alphabet.cpp -Wall -o iniAlphabet

uDebug/200UVaOJ/alphabet.cpp

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
5+
#define END std::string("#")
6+
7+
/**
8+
* Returns the index of a value contained in a given vector.
9+
*
10+
* @param value
11+
* char whose index is wanted.
12+
* @param list
13+
* vector of char where the value is contained.
14+
*
15+
* @returns int with the index of the value bounded in [0, n), where
16+
* n is the size of the vector.
17+
*/
18+
int getIndex(char value, std::vector<char> list){
19+
int i;
20+
for (i = 0; i < (int)list.size(); i++){
21+
if(value == list.at(i))
22+
return i;
23+
}
24+
25+
return -1;
26+
}
27+
28+
/**
29+
* Returns true if the incoming vector letters contains the
30+
* character searched.
31+
*
32+
* @param searched
33+
* char which may be contained in the vector.
34+
* @param letters
35+
* vector of char where the character is going to be
36+
* searched.
37+
*
38+
* @returns true if searched is contained in letters.
39+
*/
40+
bool isInTheList(char searched, std::vector<char> letters){
41+
unsigned int i;
42+
43+
for(i = 0; i < letters.size(); i++){
44+
if(letters.at(i) == searched)
45+
return true;
46+
}
47+
48+
return false;
49+
}
50+
51+
/**
52+
* Generates a vector with all the characters used in all the
53+
* words. Those characters are not repeated.
54+
*
55+
* @param words
56+
* vector of string which will be used to generate the
57+
* list of characters used.
58+
*
59+
* @returns vector of char with every character used.
60+
*/
61+
std::vector<char> lettersUsed(std::vector<std::string> words)
62+
{
63+
std::vector<char> letters;
64+
unsigned int i, j;
65+
66+
for(i = 0; i < words.size(); i++){
67+
for(j = 0; j < words.at(i).size(); j++){
68+
if(!isInTheList(words.at(i).at(j), letters)){
69+
letters.push_back(words.at(i).at(j));
70+
}
71+
}
72+
}
73+
74+
return letters;
75+
}
76+
77+
int getIndexOfRoot(bool **tree, int size){
78+
int i, j;
79+
for(j = 0; j < size; j++){
80+
for (i = 0; i < size; i++){
81+
if(tree[i][j])
82+
continue;
83+
return i;
84+
}
85+
}
86+
return -1;
87+
}
88+
89+
bool isLastNode(bool **tree, int size, int node){
90+
int i;
91+
for(i = 0; i < size; i++){
92+
if(tree[node][i])
93+
return false;
94+
}
95+
return true;
96+
}
97+
98+
bool contains(std::vector<int> parents, int node){
99+
int i;
100+
101+
for(i = 0; i < (int)parents.size(); i++){
102+
if(parents.at(i) == node)
103+
return true;
104+
}
105+
return false;
106+
}
107+
108+
bool isTheOnlyFather(bool **tree, int size, int son, int father,
109+
std::vector<int> parents){
110+
int i;
111+
112+
for(i = 0; i < size; i++){
113+
//if this father is not the given father
114+
//and
115+
//this father is not in the list
116+
//return false
117+
if(tree[i][son] && i != father && !contains(parents, i)){
118+
return false;
119+
}
120+
}
121+
return true;
122+
}
123+
124+
void sortTree(bool **tree, int size, std::vector<char> letters)
125+
{
126+
/*Rules:
127+
* + Each node must be (1,1) at the end, except
128+
* for node n0 which must be (0,1) and node nN, which
129+
* must be (1,0)
130+
* + Once a node is read, it's added to the list of
131+
* read elements R
132+
*
133+
* Process:
134+
* + Searches n0 by searching for a (0,1) node.
135+
* + nX = n0
136+
* + Loop:
137+
* + Searches for a son node nX+1 of nX
138+
* whose only father out of R is nX
139+
* + Stores nX in R
140+
* + If(nX+1 didn't appear)
141+
* + Congrats! It is nN
142+
* + End of the sorting
143+
* + nX+1 now is nX
144+
* + R contains the list
145+
*/
146+
int i;
147+
int sonNode = -1;
148+
int fatherNode = getIndexOfRoot(tree, size);
149+
std::vector<int> r;
150+
151+
do {
152+
for(i = 0; i < size; i++){
153+
if(tree[fatherNode][i] && isTheOnlyFather(tree, size, i,
154+
fatherNode, r))
155+
sonNode = i;
156+
}
157+
r.push_back(fatherNode);
158+
fatherNode = sonNode;
159+
} while(!isLastNode(tree, size, sonNode));
160+
r.push_back(fatherNode);
161+
162+
for(i = 0; i < size; i++){
163+
std::cout << letters.at(r.at(i));
164+
}
165+
std::cout << '\n';
166+
}
167+
168+
169+
void fillBoolTree(bool **tree, int size, std::vector<std::string> words,
170+
std::vector<char> letters)
171+
{
172+
/*
173+
* 1: write down links between nodes
174+
* 2: search groups of words with the same initial char
175+
* 2.1: char to temp; boolean repeated = false
176+
* 2.2: if next char is the same, temp to list L and
177+
* repeated = true
178+
* 2.3: when next char is different, repeat from 2.1
179+
* 2.4: when list is finished, take chars from L
180+
* 2.4.1: takes char C
181+
* 2.4.2: looks and inserts every word which begins
182+
* with C in list tempList
183+
* 2.4.3: removes the first char of every word
184+
* 2.4.4: sends tempList to fillBoolTree
185+
*/
186+
unsigned int i, j, indexA, indexB;
187+
char tempChar = '\0';
188+
std::vector<char> groups;
189+
std::vector<std::string> tempList;
190+
bool repeated = false;
191+
192+
// Writes down the connections between nodes
193+
for(i = 1; i < words.size(); i++){
194+
indexA = getIndex(words.at(i-1).at(0), letters);
195+
indexB = getIndex(words.at(i).at(0), letters);
196+
if(indexA != indexB)
197+
tree[indexA][indexB] = true;
198+
}
199+
200+
// Checks for groups of words and writes down the initial characters
201+
for(i = 0; i < words.size(); i++){
202+
if(words.at(i).at(0) != tempChar){
203+
tempChar = words.at(i).at(0);
204+
repeated = false;
205+
} else if(words.at(i).at(0) == tempChar && repeated == false){
206+
groups.push_back(tempChar);
207+
repeated = true;
208+
}
209+
}
210+
211+
// For every character that sets every group, generates a list and
212+
// calls fillBoolTree without the first character
213+
for(i = 0; i < groups.size(); i++){
214+
for(j = 0; j < words.size(); j++){
215+
if(groups.at(i) == words.at(j).at(0) &&
216+
words.at(j).substr(1) != ""){
217+
tempList.push_back(words.at(j));
218+
tempList.at(tempList.size()-1).
219+
erase(tempList.at(tempList.size()-1).begin());
220+
}
221+
}
222+
223+
fillBoolTree(tree, size, tempList, letters);
224+
tempList.clear();
225+
}
226+
227+
}
228+
229+
int main(void)
230+
{
231+
std::vector<std::string> words;
232+
std::string temp;
233+
unsigned int size, i, j;
234+
235+
while (std::cin >> temp && temp != END)
236+
{
237+
words.push_back(temp);
238+
}
239+
240+
std::vector<char> letters(lettersUsed(words));
241+
size = letters.size();
242+
243+
bool **tree = new bool*[size];
244+
for(i = 0; i < size; i++) // How to find array size
245+
{
246+
tree[i] = new bool[size];
247+
for(j = 0; j < size; j++)
248+
{
249+
tree[i][j] = false;
250+
}
251+
}
252+
253+
fillBoolTree(tree, size, words, letters);
254+
255+
sortTree(tree, size, letters);
256+
257+
return 0;
258+
}

uDebug/200UVaOJ/in/1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
XWY
2+
ZX
3+
ZXY
4+
ZXW
5+
YWWX
6+
#

uDebug/200UVaOJ/in/ejemplo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
R
2+
RRA
3+
RRTA
4+
RRTQ
5+
RARAQT
6+
QQ
7+
QT
8+
Q
9+
#

uDebug/200UVaOJ/iniAlphabet

68.9 KB
Binary file not shown.

uDebug/200UVaOJ/run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
make
2+
./iniAlphabet < in/ejemplo

0 commit comments

Comments
 (0)