-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataRecovery.cpp
65 lines (53 loc) · 1.33 KB
/
DataRecovery.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
//Tyler Witt
//CodeEval
//DataRecovery.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <map>
using namespace std;
string translate(string line);
int main(int argc, char *argv[]) {
ifstream stream("input.txt");
string line;
while (getline(stream, line)) {
cout << translate(line) << endl;
}
cin.get();
return 0;
}
string translate(string line){
int ploc = 0;
int wloc = 0;
string positions = line.substr(line.find(';') + 1);
string result = "";
map<int, string> dict;
int missing = 0;
line = line.substr(0, line.find(';'));
while (!positions.empty()){
ploc = positions.find(' ');
wloc = line.find(' ');
if (ploc > positions.length())
ploc = positions.length();
if (wloc > line.length())
wloc = line.length();
dict[atoi(positions.substr(0, ploc).c_str())] = line.substr(0, wloc);
if (ploc < positions.length())
positions = positions.substr(ploc + 1);
else
positions = "";
line = line.substr(wloc + 1);
}
for (int i = 1; i <= dict.size(); i++){
if (dict.find(i) == dict.end())
missing = i;
else if (missing == 0)
missing = dict.size() + 1;
}
dict[missing] = line;
for (int i = 1; i <= dict.size(); i++)
result = result + dict[i] + " ";
dict.erase(dict.begin(), dict.end());
return result;
}