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. if(first < 1)
  38. continue; //cout << 0 << "\n";
  39. else
  40. {
  41. if(first < t.size())
  42. t.resize(first);
  43. reverse(t);
  44. for(ti = 0; ti < t.size(); ti++)
  45. {
  46. //if(t[ti] != 0)
  47. cout << t[ti] << (ti == (t.size() - 1)?"":" ");
  48. }
  49. cout << "\n";
  50. }
  51. }
  52. }
  53.  
  54. friend ostream& operator<<(ostream& os, const PP0502B& f);
  55.  
  56. private:
  57. int _ns;
  58. vector< vector<int> > _vi;
  59.  
  60. private:
  61. void reverse(vector<int>& vi)
  62. {
  63. int vs, index;
  64. vs = vi.size();
  65. for(index = 0; index < vs / 2; index++)
  66. {
  67. swap(vi[index], vi[vs - index - 1]);
  68. }
  69. }
  70.  
  71. int popfront(vector<int>& vi)
  72. {
  73. int index, vs, newcapacity, firstelement;
  74.  
  75. vs = vi.size();
  76. firstelement = vi[0];
  77. newcapacity = vs - 1;
  78. vector<int> nvi(newcapacity);
  79. for(index = 1; index < vs; index++)
  80. {
  81. nvi[index-1] = vi[index];
  82. }
  83. vi.clear();
  84. vector<int>().swap(vi); // https://stackoverflow.com/a/10465032
  85. vi = nvi;
  86.  
  87. return firstelement;
  88. }
  89.  
  90. 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;}
  91.  
  92. 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;}
  93.  
  94. const char* getnumdelimiters(const char *s, int delimiter, int *ret){int numdelimiters = 0;while(*s++ != '\0'){if(*s == delimiter) numdelimiters++;}*ret = numdelimiters;return s;}
  95.  
  96. 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;}
  97.  
  98. 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;}
  99. };
  100.  
  101. ostream& operator<<(ostream& os, const PP0502B& f)
  102. {
  103. //
  104.  
  105. return os;
  106. }
  107.  
  108. int main()
  109. {
  110. int numsets;
  111. string input;
  112.  
  113. getline(cin, input);
  114. numsets = atoi(input.c_str());
  115.  
  116. PP0502B pp0502b(numsets);
  117. }
Success #stdin #stdout 0s 16072KB
stdin
6
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
4 -1 -2 -3 4 5
10 -1 -2 -3 4 5 6 7
stdout
7 6 5 4 3 2 1
11 2 3
4 3 2 1
1 2 3 4 5
4 -3 -2 -1
7 6 5 4 -3 -2 -1