Posted By


dirkchang on 02/02/11

Tagged


Statistics


Viewed 130 times
Favorited by 0 user(s)

Studious Student


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*
  2.  *
  3.  * Author: Dirk Chang (), [email protected]
  4.  *
  5.  * =====================================================================================
  6.  */
  7.  
  8. #include <string>
  9. #include <iostream>
  10. #include <fstream>
  11. #include <algorithm>
  12. #include <set>
  13. #include <iterator>
  14. #include <vector>
  15. using namespace std;
  16.  
  17. class COMP {
  18. private:
  19. enum Which {NIL, X, Y};
  20. Which _w;
  21. size_t _len;
  22.  
  23. public:
  24. COMP() : _w(NIL), _len(0) {}
  25. bool operator()(string const &x, string const &y) {
  26. _len = x.length();
  27. if(_len > y.length()) {
  28. _len = y.length();
  29. _w = Y;
  30. }
  31. else if(_len == y.length()) { // eliminate the possibility of x.length() == y.length()
  32. return x < y;
  33. }
  34. else {
  35. _w = X;
  36. }
  37.  
  38. for(size_t i = 0; i < _len; ++i) {
  39. if(x[i]==y[i]) continue;
  40. else return x[i] < y[i];
  41. }
  42.  
  43. if(_w == X) {
  44. return y[0] < y[_len];
  45. }
  46. else {
  47. return x[_len] < x[0];
  48. }
  49. }
  50. };
  51.  
  52. int main(int argc, char **argv) {
  53. COMP comp;
  54. vector<string> *vec;
  55. string input;
  56. int n, subn;
  57.  
  58. ifstream fin(argv[1]);
  59. fin >> n;
  60. vec = new vector<string> [n];
  61. for(int i = 0; i < n; ++i) {
  62. fin >> subn;
  63. vec[i].resize(subn);
  64. for(int j = 0; j < subn; ++j) {
  65. fin >> vec[i][j];
  66. }
  67. sort(vec[i].begin(), vec[i].end(), comp);
  68. copy(vec[i].begin(), vec[i].end(), ostream_iterator<string>(cout, ""));
  69. cout << '\n';
  70. }
  71.  
  72. delete [] vec;
  73. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.