fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <cstddef>
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <cstring>
  9. #include <sstream>
  10.  
  11. // 29.01.18
  12. // PP0502B
  13.  
  14. using namespace std;
  15.  
  16. class PP0502B {
  17. public:
  18. PP0502B(int numsets) : _ns(numsets)
  19. {
  20. int index, delimiter = ' ';
  21. string sset;
  22. vector<int> a;
  23. for(index = 0; index < _ns; index++)
  24. {
  25. getline(cin, sset);
  26. a = explode(sset, delimiter);
  27. cin.clear();
  28. _vi.push_back(a);
  29. }
  30.  
  31. vector<int> t;
  32. int first = 0, ti;
  33. for(index = 0; index < _ns; index++)
  34. {
  35. t = _vi[index];
  36. first = popfront(t);
  37. t.resize(first);
  38. reverse(t);
  39. for(ti = 0; ti < t.size(); ti++)
  40. {
  41. if(t[ti] != 0) {
  42. cout << t[ti];
  43. cout << (ti == (t.size() - 1)?"":" ");
  44. }
  45. }
  46. cout << "\n";
  47. }
  48. }
  49.  
  50. friend ostream& operator<<(ostream& os, const PP0502B& f);
  51.  
  52. private:
  53. int _ns; // liczba zbiorow
  54. vector< vector<int> > _vi; // wektor wektorow liczb
  55.  
  56. private:
  57. void reverse(vector<int>& vi)
  58. {
  59. int vs, index;
  60. vs = vi.size();
  61. for(index = 0; index < vs / 2; index++)
  62. {
  63. swap(vi[index], vi[vs - index - 1]); // odwracanie
  64. }
  65. }
  66.  
  67. int popfront(vector<int>& vi)
  68. {
  69. int index, vs, newcapacity, firstelement;
  70.  
  71. vs = vi.size();
  72. firstelement = vi[0]; // zdejmuje pierwsza liczbe
  73. newcapacity = vs - 1;
  74. vector<int> nvi(newcapacity);
  75. for(index = 1; index < vs; index++)
  76. {
  77. nvi[index-1] = vi[index]; // kopiuje do nowego w.
  78. }
  79. vi.clear(); // czysci stary
  80. vector<int>().swap(vi); // https://stackoverflow.com/a/10465032
  81. vi = nvi; // referencja
  82.  
  83. return firstelement;
  84. }
  85.  
  86. // funkcje pomocnicze
  87. // wycina X znakow z ciagu tekstowego i konwertujemy do int
  88. int strtoint(const char *string, int tocopy){int number;char *t;t = (char *)malloc(sizeof(char) * tocopy);memcpy(t, string, tocopy);number = atoi(static_cast<const char*>(t));free(t);return number;}
  89. // obcina space z lewa i prawa
  90. char* trimstring(const char *sp){const char *p, *q;char *t;int length, temp;p = q = sp;while(*q != '\0') {q++;};while(*p == ' '){p++;};while(*q == ' ' || *q == '\0') {q--;}q++;length = q - p;t = (char *)malloc(sizeof(char) * length);memcpy(t, p, length);return t;}
  91. // pobiera liczbe oddzielajacych ciag tekstowy znakow (tutj: ' ')
  92. const char* getnumdelimiters(const char *s, int delimiter, int *ret){int numdelimiters = 0;while(*s++ != '\0'){if(*s == delimiter) numdelimiters++;}*ret = numdelimiters;return s;}
  93. // usuwa zbedne ' ' pomiedzy znakami (zostawia jedno)
  94. const char* convertstringtorightformat(const char *sp, int delimiter){const char *p, *q;char *s, *t;int length, first = 1, index = 0, temp = 0;s = trimstring(sp);q = p = s;while(*q++ != '\0'){if(*q == delimiter){q++;}};length = q - p;t = (char *)malloc(sizeof(char) * length);while(p != q){if(*p == delimiter){if(first == 1){first = 0;memcpy(&t[index], p, sizeof(char));}while(*p == delimiter){temp++;p++;}if(temp > 0){p--;}} else {first = 1;memcpy(&t[index], p, sizeof(char));}index++;p++;}return t;}
  95. // konwertuje np. '1 2 3 4 5 6 7 8' do wektora int'ow
  96. vector<int> explode(string& inputs, int delimiter){vector<int> ret;const char *p, *q, *s;int numdelimiters = 0, index, tocopy, number;s = strdup(inputs.c_str());s = convertstringtorightformat(s, delimiter);q = p = s;s = getnumdelimiters(s, delimiter, &numdelimiters);for(index = 0; index < numdelimiters; index++){while(*q++ != delimiter);tocopy = q - p;if(tocopy < 1){break;}; number = strtoint(p, tocopy);ret.push_back(number);p = q;}if(p < s){tocopy = s - p - 1;number = strtoint(p, tocopy);ret.push_back(number);}return ret;}
  97. };
  98.  
  99. ostream& operator<<(ostream& os, const PP0502B& f)
  100. {
  101. //
  102.  
  103. return os;
  104. }
  105.  
  106. int main()
  107. {
  108. int numsets;
  109. string input;
  110.  
  111. getline(cin, input);
  112. numsets = atoi(input.c_str());
  113.  
  114. PP0502B pp0502b(numsets);
  115. }
Success #stdin #stdout 0s 4532KB
stdin
4
7 1 2 3 4 5 6 7
3 3 2 11
4 1 2 3 4 5 6 7 8
10 5 4 3 2 1

stdout
7 6 5 4 3 2 1
11 2 3
4 3 2 1
1 2 3 4 5