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