Skip to content

Instantly share code, notes, and snippets.

@cengiz-io
Last active August 29, 2015 14:11
Show Gist options
  • Save cengiz-io/ce973bfbf4e210f9fd4c to your computer and use it in GitHub Desktop.
Save cengiz-io/ce973bfbf4e210f9fd4c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <set>
#include <iterator>
#ifndef __APPLE__
#include <algorithm>
#endif
using namespace std;
struct Number {
size_t index;
int value;
Number(const int value, const size_t index) {
this->value = value;
this->index = index;
}
Number(const int value) {
this->value = value;
this->index = -1;
}
bool operator== (const Number& other) const {
return (other.value == this->value);
}
bool operator< (const Number& other) const {
return (other.value < this->value);
}
};
int main(int argc, char *argv[]) {
ifstream datafile(argv[1]);
string line;
while (getline(datafile, line)) {
if (line.length() < 1) {
cout << endl;
continue;
}
vector<int> numbers;
set<Number> check;
istringstream stream(line);
string token;
while (getline(stream, token, ' ')) {
istringstream converter(token);
int number;
converter >> number;
numbers.push_back(number);
};
ostringstream out;
size_t loop_length = 0;
for (vector<int>::iterator it = numbers.begin(); it < numbers.end(); ++it) {
int number = *it;
Number candidate = Number(number);
set<Number>::iterator existing = check.find(candidate);
if (existing != check.end()) {
for (size_t x = (*existing).index; x < loop_length; ++x) {
out << numbers.at(x) << " ";
}
break;
}
check.insert(Number(number, loop_length));
loop_length++;
}
out << " ";
string result = out.str();
result.resize(result.length() - 2);
cout << result << endl;
}
return 0;
}
1 2 3 4 5 6 7 8 9 11 11 11 11
1 2 3 4 5 6 7 8 9 11 11 11 11 4 5 6
99 98 97 96 95 94 93 92 91 95 94 93 92 91 90
4 57 57 57 57 57 57 57 57 57 57 57 57 57 57
2 0 6 3 1 6 3 1 6 3 1
3 4 8 0 11 9 7 2 5 6 10 1 49 49 49 49
1 2 3 1 2 3 1 2 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment