/ Published in: C++
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* * * Author: Dirk Chang (), [email protected] * * ===================================================================================== */ #include <string> #include <iostream> #include <fstream> #include <algorithm> #include <set> #include <iterator> #include <vector> using namespace std; class COMP { private: enum Which {NIL, X, Y}; Which _w; size_t _len; public: COMP() : _w(NIL), _len(0) {} bool operator()(string const &x, string const &y) { _len = x.length(); if(_len > y.length()) { _len = y.length(); _w = Y; } else if(_len == y.length()) { // eliminate the possibility of x.length() == y.length() return x < y; } else { _w = X; } for(size_t i = 0; i < _len; ++i) { if(x[i]==y[i]) continue; else return x[i] < y[i]; } if(_w == X) { return y[0] < y[_len]; } else { return x[_len] < x[0]; } } }; int main(int argc, char **argv) { COMP comp; vector<string> *vec; string input; int n, subn; ifstream fin(argv[1]); fin >> n; vec = new vector<string> [n]; for(int i = 0; i < n; ++i) { fin >> subn; vec[i].resize(subn); for(int j = 0; j < subn; ++j) { fin >> vec[i][j]; } sort(vec[i].begin(), vec[i].end(), comp); copy(vec[i].begin(), vec[i].end(), ostream_iterator<string>(cout, "")); cout << '\n'; } delete [] vec; }