fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Pomysl {
  6.  
  7. string nazwa;
  8. unsigned CNT[10];
  9. Pomysl *next;
  10. Pomysl *prev;
  11. Pomysl(string nazwa, unsigned CNT[]);
  12.  
  13. };
  14.  
  15. struct Stos {
  16. Pomysl *first; // pierwszy element
  17. Pomysl *last; // ostatni element
  18. Pomysl *start; // poczatek stosu
  19. unsigned wzor[10]; // zbiór startowy
  20.  
  21. // Metody:
  22. Stos();
  23. void insert_in_stack(string nazwa, unsigned CNT[]);
  24. void reverse_stack();
  25. void find_pomysl();
  26. };
  27.  
  28. Pomysl::Pomysl(string nazwa, unsigned CNT[]) {
  29. this->nazwa = nazwa;
  30. for (int i = 0; i < 10; i++) {
  31. this->CNT[i] = CNT[i];
  32. }
  33. next = NULL;
  34. prev = NULL;
  35. }
  36.  
  37. Stos::Stos() {
  38. first = NULL;
  39. last = NULL;
  40. start = NULL;
  41. }
  42.  
  43. void Stos::insert_in_stack(string nazwa, unsigned CNT[]) {
  44. Pomysl *nowy = new Pomysl(nazwa, CNT);
  45. if (first == NULL) { // Pierwszy element listy
  46. first = nowy;
  47. last = nowy;
  48. start = last;
  49. }
  50. else if (start == last) {
  51. nowy->prev = last;
  52. last->next = nowy;
  53. last = nowy;
  54. start = last;
  55. }
  56. else if (start == first) {
  57. nowy->next = first;
  58. first->prev = nowy;
  59. first = nowy;
  60. start = first;
  61. }
  62. }
  63.  
  64. void Stos::reverse_stack() {
  65. if (start == last) {
  66. start = first;
  67. }
  68. else if (start == first) {
  69. start = last;
  70. }
  71. }
  72.  
  73. void Stos::find_pomysl() {
  74. unsigned more = 0;
  75. Pomysl *tmp1 = last;
  76. Pomysl *tmp2 = first;
  77.  
  78. if (start == last) {
  79. while (/*more >= 3 ||*/ tmp1 != NULL) {
  80. for (unsigned i = 0; i<10; ++i) more += (wzor[i]<tmp1->CNT[i]);
  81. if (more >= 3) { // pasuje
  82. cout << tmp1->nazwa << endl; // wyświetla nazwę
  83. for (int i = 0; i < 10; i++) {
  84. wzor[i] = tmp1->CNT[i];
  85. }
  86. // i usuwa:
  87. if (tmp1 == last) {
  88. last->prev->next = NULL;
  89. last = last->prev;
  90. start = last;
  91. break;
  92. }
  93. else if (tmp1 == first) {
  94. first->next->prev = NULL;
  95. first = first->next;
  96. break;
  97. }
  98. else { // w środku
  99. tmp1->prev->next = tmp1->next;
  100. tmp1->next->prev = tmp1->prev;
  101. break;
  102. }
  103. }
  104. else { // znajduje niepasujący
  105. // usuwa:
  106. if (tmp1 == last) {
  107. last->prev->next = NULL;
  108. last = last->prev;
  109. start = last;
  110. }
  111. else if (tmp1 == first) {
  112. first->next->prev = NULL;
  113. first = first->next;
  114. }
  115. else { // w środku
  116. tmp1->prev->next = tmp1->next;
  117. tmp1->next->prev = tmp1->prev;
  118. }
  119. }
  120. tmp1 = tmp1->prev;
  121. }
  122. }
  123. else if (start == first) {
  124. while (/*more >= 3 || */tmp2 != NULL) {
  125. for (unsigned i = 0; i<10; ++i) more += (wzor[i]<tmp2->CNT[i]);
  126. if (more >= 3) { // pasuje
  127. cout << tmp2->nazwa << endl; // wyświetla nazwę
  128. for (int i = 0; i < 10; i++) {
  129. wzor[i] = tmp2->CNT[i];
  130. }
  131. // i usuwa:
  132. if (tmp2 == first) {
  133. first->next->prev = NULL;
  134. first = first->next;
  135. start = first;
  136. break;
  137. }
  138. else if (tmp2 == last) {
  139. last->prev->next = NULL;
  140. last = last->prev;
  141. break;
  142. }
  143. else { // w środku
  144. tmp2->prev->next = tmp2->next;
  145. tmp2->next->prev = tmp2->prev;
  146. break;
  147. }
  148. }
  149. else { // znajduje niepasujący
  150. // usuwa:
  151. if (tmp2 == last) {
  152. last->prev->next = NULL;
  153. last = last->prev;
  154. }
  155. else if (tmp2 == first) {
  156. first->next->prev = NULL;
  157. first = first->next;
  158. start = first;
  159. }
  160. else { // w środku
  161. tmp2->prev->next = tmp2->next;
  162. tmp2->next->prev = tmp2->prev;
  163. }
  164. }
  165. tmp2 = tmp2->next;
  166. }
  167. }
  168. else {
  169. cout << "NIE" << endl;
  170. }
  171. }
  172.  
  173. int main() {
  174.  
  175. int n; // liczba operacji
  176. int s; // liczba cyfr w opisie startowym
  177. string nazwa; // nazwa pomysłu
  178. unsigned m; // liczba cyfr w opisie pomysłu
  179. int op; // rodzaj operacji
  180.  
  181. Stos *stos = new Stos;
  182.  
  183. cin >> n;
  184.  
  185. unsigned stan[10] = {}; // stan startowy
  186. unsigned v = 0; // pomocniczo el. tablicy
  187. for (cin >> s; s--; ++stan[v]) cin >> v;
  188. for (int i = 0; i < 10; i++) {
  189. stos->wzor[i] = stan[i];
  190. }
  191.  
  192.  
  193. for (int i = 0; i < n; i++) {
  194. cin >> op;
  195. if (op == 1) {
  196. // operacja dodawania pomysłu
  197. cin.ignore();
  198. getline(cin, nazwa);
  199. unsigned CNT[10] = {};
  200. unsigned V = 0; // pomocniczo el. tablicy
  201. for (cin >> m; m--; ++CNT[V]) cin >> V;
  202. // funkcja dodająca
  203. stos->insert_in_stack(nazwa, CNT);
  204. }
  205. else if (op == 2) {
  206. // zmiana kolejności
  207. stos->reverse_stack();
  208. }
  209. else if (op == 3) {
  210. // poszukiwanie pomysłu
  211. stos->find_pomysl();
  212. }
  213. }
  214. /*
  215. for (int i = 0; i < 10; i++) {
  216. cout << stos->first->CNT[i] << endl;
  217. }
  218. cout << "wzor:" << endl;
  219. for (int i = 0; i < 10; i++) {
  220. cout << stos->wzor[i] << endl;
  221. }*/
  222.  
  223. return 0;
  224. }
Time limit exceeded #stdin #stdout 5s 3476KB
stdin
Standard input is empty
stdout
Standard output is empty