-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8_namenum.cpp
161 lines (129 loc) · 3.94 KB
/
8_namenum.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
ID: 27ckuzm1
TASK: namenum
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <algorithm>
using namespace std;
int getDigit(string n, int digit) {
return n[digit] - '2';
}
string makeString(vector< vector<char> >& reference, int& length, string& number, vector<int>& iterationsArray) {
string word;
int iterate = 0;
while (iterate < length) {
word.push_back( reference[getDigit(number, iterate)][iterationsArray[iterate]] );
++iterate;
}
return word;
}
void getPossibleNames(string& number, vector< vector<char> >& reference, ifstream& dict, vector< vector<string> >& possibleNames){
dict.open("dict.txt");
string line;
bool found = false;
while ( getline(dict, line) ) {
if ( line[0] == reference[getDigit(number, 0)][0] )
possibleNames[0].push_back(line);
if ( line[0] == reference[getDigit(number, 0)][1] )
possibleNames[1].push_back(line);
if ( line[0] == reference[getDigit(number, 0)][2] )
possibleNames[2].push_back(line);
}
dict.close();
}
bool checkName(string& name, vector< vector<string> >& possibleNames) {
if ( find(possibleNames[0].begin(), possibleNames[0].end(), name) != possibleNames[0].end() )
return true;
if ( find(possibleNames[1].begin(), possibleNames[1].end(), name) != possibleNames[1].end() )
return true;
if ( find(possibleNames[2].begin(), possibleNames[2].end(), name) != possibleNames[2].end() )
return true;
return false;
}
void printAllNames(const string& number, const vector<vector<char>>& reference, vector<string>& output, vector< vector<string> >& possibleNames) {
int digits = number.size();
vector<int> indices(digits, 0);
while (true) {
string word;
for (int i = 0; i < digits; ++i) {
word.push_back(reference[getDigit(number, i)][indices[i]]);
}
if (checkName(word, possibleNames)) {
output.push_back(word);
}
// Increment indices
int i = digits - 1;
while (i >= 0 && ++indices[i] == 3) {
indices[i] = 0;
--i;
}
// Check if all indices have been exhausted
if (i < 0) {
break;
}
}
}
int main() {
time_t start, end;
time(&start);
// get input from file;
ifstream inputFile;
inputFile.open("namenum.in");
string stringNum;
getline(inputFile, stringNum);
inputFile.close();
// define variables;
vector<string> names;
vector< vector<char> > numbers{
{'A', 'B', 'C'},
{'D', 'E', 'F'},
{'G', 'H', 'I'},
{'J', 'K', 'L'},
{'M', 'N', 'O'},
{'P', 'R', 'S'},
{'T', 'U', 'V'},
{'W', 'X', 'Y'}
};
int digits = stringNum.size();
int totalIterations = pow(3, digits);
vector<int> i;
for (int j = 0; j < digits; ++j) {
i.push_back(0);
}
// fill vector will all possible names;
vector< vector<string> > possibleNames{
vector<string>(),
vector<string>(),
vector<string>()
};
ifstream dict;
getPossibleNames(stringNum, numbers, dict, possibleNames);
printAllNames(stringNum, numbers, names, possibleNames);
// check each name with file;
vector<string> goodNames;
numbers.clear();
numbers.shrink_to_fit();
possibleNames.clear();
possibleNames.shrink_to_fit();
// return good ones;
ofstream outputFile;
outputFile.open("namenum.out");
if (names.size() == 0) {
outputFile << "NONE" << endl;
} else {
for (int i = 0; i < names.size(); ++i) {
outputFile << names[i] << endl;
}
}
outputFile.close();
time(&end);
double time_taken = double(end - start);
cout << "Time taken by program is : " << time_taken << " seconds" << endl;
return 0;
}