fork 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. cout << t[ti] << (ti == (t.size() - 1)?"":" ");
  42. }
  43. cout << "\n";
  44. }
  45. }
  46.  
  47. friend ostream& operator<<(ostream& os, const PP0502B& f);
  48.  
  49. private:
  50. int _ns;
  51. vector< vector<int> > _vi;
  52.  
  53. private:
  54. void reverse(vector<int>& vi)
  55. {
  56. int vs, index;
  57. vs = vi.size();
  58. for(index = 0; index < vs / 2; index++)
  59. {
  60. swap(vi[index], vi[vs - index - 1]);
  61. }
  62. }
  63.  
  64. int popfront(vector<int>& vi)
  65. {
  66. int index, vs, newcapacity, firstelement;
  67.  
  68. vs = vi.size();
  69. firstelement = vi[0];
  70. newcapacity = vs - 1;
  71. vector<int> nvi(newcapacity);
  72. for(index = 1; index < vs; index++)
  73. {
  74. nvi[index-1] = vi[index];
  75. }
  76. vi.clear();
  77. vector<int>().swap(vi); // https://stackoverflow.com/a/10465032
  78. vi = nvi;
  79.  
  80. return firstelement;
  81. }
  82.  
  83. int strtoint(const char *string, int tocopy)
  84. {
  85. int number;
  86. char *t;
  87.  
  88. t = (char *)malloc(sizeof(char) * tocopy);
  89. memcpy(t, string, tocopy);
  90. number = atoi(static_cast<const char*>(t));
  91. free(t);
  92.  
  93. return number;
  94. }
  95.  
  96. char* trimstring(const char *sp)
  97. {
  98. const char *p, *q;
  99. char *t;
  100. int length, temp;
  101.  
  102. p = q = sp;
  103. while(*q != '\0')
  104. {
  105. q++;
  106. }
  107. while(*p == ' ')
  108. {
  109. p++;
  110. }
  111. while(*q == ' ' || *q == '\0') {
  112. q--;
  113. }
  114. q++;
  115. length = q - p;
  116. t = (char *)malloc(sizeof(char) * length);
  117. memcpy(t, p, length);
  118.  
  119. return t;
  120. }
  121.  
  122. const char* getnumdelimiters(const char *s, int delimiter, int *ret)
  123. {
  124. int numdelimiters = 0;
  125.  
  126. while(*s++ != '\0')
  127. {
  128. if(*s == delimiter)
  129. numdelimiters++;
  130. }
  131. *ret = numdelimiters;
  132.  
  133. return s;
  134. }
  135.  
  136. const char* convertstringtorightformat(const char *sp, int delimiter)
  137. {
  138. const char *p, *q;
  139. char *s, *t;
  140. int length, first = 1, index = 0, temp = 0;
  141.  
  142. s = trimstring(sp);
  143. q = p = s;
  144. while(*q++ != '\0')
  145. {
  146. if(*q == delimiter)
  147. {
  148. q++;
  149. }
  150. }
  151. length = q - p;
  152. t = (char *)malloc(sizeof(char) * length);
  153. while(p != q)
  154. {
  155. if(*p == delimiter)
  156. {
  157. if(first == 1)
  158. {
  159. first = 0;
  160. memcpy(&t[index], p, sizeof(char));
  161. }
  162. while(*p == delimiter)
  163. {
  164. temp++;
  165. p++;
  166. }
  167. if(temp > 0)
  168. {
  169. p--;
  170. }
  171. } else
  172. {
  173. first = 1;
  174. memcpy(&t[index], p, sizeof(char));
  175. }
  176. index++;
  177. p++;
  178. }
  179.  
  180. return t;
  181. }
  182.  
  183. vector<int> explode(string& inputs, int delimiter)
  184. {
  185. vector<int> ret;
  186. const char *p, *q, *s;
  187. int numdelimiters = 0, index, tocopy, number;
  188.  
  189. s = strdup(inputs.c_str());
  190. s = convertstringtorightformat(s, delimiter);
  191. q = p = s;
  192. s = getnumdelimiters(s, delimiter, &numdelimiters);
  193. for(index = 0; index < numdelimiters; index++)
  194. {
  195. while(*q++ != delimiter);
  196. tocopy = q - p;
  197. if(tocopy < 1)
  198. {
  199. break;
  200. }
  201. number = strtoint(p, tocopy);
  202. ret.push_back(number);
  203. p = q;
  204. }
  205. if(p < s)
  206. {
  207. tocopy = s - p - 1;
  208. number = strtoint(p, tocopy);
  209. ret.push_back(number);
  210. }
  211.  
  212. return ret;
  213. }
  214. };
  215.  
  216. ostream& operator<<(ostream& os, const PP0502B& f)
  217. {
  218. //
  219.  
  220. return os;
  221. }
  222.  
  223. int main()
  224. {
  225. int numsets;
  226. string input;
  227.  
  228. getline(cin, input);
  229. numsets = atoi(input.c_str());
  230.  
  231. PP0502B pp0502b(numsets);
  232. }
Success #stdin #stdout 0s 4260KB
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
0 0 0 0 0 1 2 3 4 5