fork download
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. #include<string>
  5. #include<iostream>
  6. using namespace std;
  7. class vector{
  8. string* tab;
  9. int capacity;
  10. int size;
  11. public:
  12. vector(int a=4,int b=0,string c="")
  13. {
  14. capacity=a;
  15. size=b;
  16. tab=new string[a];
  17. for(int i=0;i<b;i++)
  18. {
  19. tab[i]=c;
  20. }
  21. }
  22. vector(vector &x)
  23. {
  24. capacity=x.capacity;
  25. size=x.size;
  26. tab=new string[x.capacity];
  27. for(int i=0;i<x.capacity;i++)
  28. {
  29. tab[i]=x.tab[i];
  30. }
  31. }
  32. ~vector()
  33. {
  34. delete[] tab;
  35. }
  36. void add(string x)
  37. {
  38. if(capacity>size)
  39. {
  40. tab[size]=x;
  41. size++;
  42. }
  43. else
  44. {
  45. capacity=capacity*2;
  46. string* tab2=new string[capacity];
  47. for(int i=0;i<size;i++)
  48. {
  49. tab2[i]=tab[i];
  50. }
  51. delete[] tab;
  52. tab=tab2;
  53. tab[size]=x;
  54. size++;
  55. }
  56. }
  57. int getSize()
  58. {
  59. return size;
  60. }
  61. void resize(int s)
  62. {
  63. if(capacity>=s)
  64. {
  65. for(int i=size;i<s;i++)
  66. {
  67. tab[i]="";
  68. }
  69. size=s;
  70. }
  71. else
  72. {
  73. while(capacity<s)
  74. capacity=capacity*2;
  75. string* tab2=new string[capacity];
  76. for(int i=0;i<size;i++)
  77. {
  78. tab2[i]=tab[i];
  79. }
  80. delete[] tab;
  81. tab=tab2;
  82. for(int i=size;i<s;i++)
  83. {
  84. tab[i]="";
  85. }
  86. size=s;
  87. }
  88. }
  89. void clear()
  90. {
  91. size=0;
  92. }
  93. void insert(int i,string x)
  94. {
  95. if(i<=size)
  96. {
  97. if(size<capacity)
  98. {
  99. for(int j=size-1;j>=i;j--)
  100. {
  101. tab[j+1]=tab[j];
  102. }
  103. tab[i]=x;
  104. size++;
  105. }
  106. else
  107. {
  108. capacity=capacity*2;
  109. string* tab2=new string[capacity];
  110. for(int j=0;j<size;j++)
  111. {
  112. tab2[j]=tab[j];
  113. }
  114. delete[] tab;
  115. tab=tab2;
  116. for(int j=size-1;j>=i;j--)
  117. {
  118. tab[j+1]=tab[j];
  119. }
  120. tab[i]=x;
  121. size++;
  122. }
  123. }
  124. }
  125. void erase(int i)
  126. {
  127. if(i<=size&&size>0)
  128. {
  129. for(int j=i;j<size-1;j++)
  130. {
  131. tab[j]=tab[j+1];
  132. }
  133. size--;
  134. }
  135. }
  136. string &operator[](int x)
  137. {
  138. if(x>=size)
  139. return tab[size-1];
  140. else
  141. return tab[x];
  142. }
  143. vector &operator=(vector x)
  144. {
  145. capacity=x.capacity;
  146. size=x.size;
  147. delete[] tab;
  148. tab=new string[capacity];
  149. for(int i=0;i<size;i++)
  150. {
  151. tab[i]=x.tab[i];
  152. }
  153. }
  154. friend ostream& operator<<(ostream &stream, vector &A)
  155. {
  156. for(int i=0;i<A.size;i++)
  157. {
  158. stream << A.tab[i];
  159. stream << " ";
  160. }
  161. return stream;
  162. }
  163. friend istream& operator>>(istream &stream, vector &A)
  164. {
  165. for(int i=0;i<A.size;i++)
  166. {
  167. stream >> A.tab[i];
  168. }
  169. return stream;
  170. }
  171. };
  172.  
  173.  
  174. int main()
  175. {
  176. int z, a, x;
  177. string y;
  178. string s;
  179.  
  180. cin >> z;
  181. while (z--)
  182. {
  183. vector V;
  184. cin >> a;
  185. while (a--)
  186. {
  187. cin >> s;
  188. //cout << s << endl;
  189. switch(s[0])
  190. {
  191. case 'a':
  192. cin >> y;
  193. V.add( y );
  194. break;
  195. case 'i':
  196. {
  197. cin >> x >> y;
  198. V.insert( x, y );
  199. break;
  200. }
  201. case 'e':
  202. {
  203. cin >> x;
  204. V.erase( x );
  205. break;
  206. }
  207. case 'c':
  208. V.clear();
  209. break;
  210. case 'r':
  211. int r;
  212. cin >> r;
  213. V.resize(r);
  214. break;
  215. case 's': //set i size
  216. if (s[1]=='i') { cout << V.getSize() << endl; break; }
  217. cin >> x >> y;
  218. if (V.getSize()==0) break;
  219. V[x] = y;
  220. break;
  221. case 'g':
  222. cin >> x;
  223. if (V.getSize()==0) break;
  224. cout << V[x] << endl;
  225. break;
  226. case 'p':
  227. cout << V << endl;
  228. break;
  229.  
  230. };
  231. }
  232. }
  233. return 0;
  234. }
Success #stdin #stdout 0s 3476KB
stdin
1
100
add SDVNWKT
add ESXFF
erase 1
add S
erase 1
insert 4 YIBYZMO
print
set 2 MCGOMNIWOO
add JJD
add JT
insert 2 SXLGNWNJF
add FOGDNNTAY
insert 5 O
clear
size
print
erase 11
insert 12 PTVNT
insert 3 ICHNBIXRIB
size
get 5
erase 0
erase 15
set 17 CA
print
erase 11
erase 20
erase 0
erase 8
erase 1
add YQOG
add PMNKUVVONB
set 24 ZBXTFT
add PREWIIRSI
add YGFJWFWDY
insert 5 IDCMLZK
add C
add LT
print
get 17
add CFUBAWEFID
set 28 WSAO
add OHDSBIBDFW
add V
insert 4 WYCKREE
size
insert 23 OUXUK
get 43
add RFAFYQF
clear
insert 33 EM
print
erase 47
clear
size
clear
get 18
clear
set 9 O
erase 24
erase 20
add HYNKTPSCIP
erase 45
erase 35
print
add WZEKSFBXPU
insert 2 TD
set 41 QYMPXH
get 20
print
insert 4 T
insert 35 GPPELRVN
erase 41
erase 14
add BZGSGT
get 20
get 23
size
set 6 HAA
add KZWSD
get 40
add ACYFKFFKV
add S
erase 79
add CP
size
add IGH
insert 27 YTQSTFNJQ
insert 12 NOSIYTWN
clear
size
add UTCSGPGAYF
erase 68
add MKSMEIKUQX
add P
get 16
print
erase 67
get 38
insert 95 NL
stdout
SDVNWKT 
0

0

YQOG ZBXTFT PREWIIRSI YGFJWFWDY C LT 
LT
10
V

0
HYNKTPSCIP 
QYMPXH
HYNKTPSCIP WZEKSFBXPU QYMPXH 
BZGSGT
BZGSGT
4
KZWSD
8
0
P
UTCSGPGAYF MKSMEIKUQX P 
P