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 delete_pomysl(Pomysl *x);
  26. void find_pomysl();
  27. };
  28.  
  29. Pomysl::Pomysl(string nazwa, unsigned CNT[]) {
  30. this->nazwa = nazwa;
  31. for (int i = 0; i < 10; i++) {
  32. this->CNT[i] = CNT[i];
  33. }
  34. next = NULL;
  35. prev = NULL;
  36. }
  37.  
  38. Stos::Stos() {
  39. first = NULL;
  40. last = NULL;
  41. start = NULL;
  42. }
  43.  
  44. void Stos::insert_in_stack(string nazwa, unsigned CNT[]) {
  45. Pomysl *nowy = new Pomysl(nazwa, CNT);
  46. if (first == NULL) { // Pierwszy element listy
  47. first = nowy;
  48. last = nowy;
  49. start = last;
  50. }
  51. else if (start == last) {
  52. nowy->prev = last;
  53. if (last != NULL) {
  54. last->next = nowy;
  55. }
  56. last = nowy;
  57. nowy->next = NULL;
  58. }
  59. else if (start == first) {
  60. nowy->next = first;
  61. if (first != NULL) {
  62. first->prev = nowy;
  63. }
  64. first = nowy;
  65. nowy->prev = NULL;
  66. }
  67. }
  68.  
  69. void Stos::reverse_stack() {
  70. if (start == last) {
  71. start = first;
  72. }
  73. else if (start == first) {
  74. start = last;
  75. }
  76. }
  77.  
  78. void Stos::delete_pomysl(Pomysl *x) {
  79. if (x->prev != NULL) {
  80. x->prev->next = x->next;
  81. }
  82. else {
  83. first = x->next;
  84. }
  85. if (x->next != NULL) {
  86. x->next->prev = x->prev;
  87. }
  88. else {
  89. last = x->prev;
  90. }
  91. }
  92.  
  93. void Stos::find_pomysl() {
  94. unsigned more = 0;
  95. if (start == last) {
  96. Pomysl *pomysl = last;
  97. while (pomysl != NULL && more < 3) {
  98. for (unsigned i = 0; i<10; ++i) more += (wzor[i]<pomysl->CNT[i]);
  99. if (more >= 3) { // pasuje
  100. cout << pomysl->nazwa << endl; // wyświetla nazwę
  101. for (int i = 0; i < 10; i++) {
  102. wzor[i] = pomysl->CNT[i];
  103. }
  104. // i usuwa:
  105. delete_pomysl(pomysl);
  106. }
  107. else { // znajduje niepasujący
  108. // usuwa:
  109. delete_pomysl(pomysl);
  110. }
  111. pomysl = pomysl->prev;
  112. }
  113. }
  114. else if (start == first) {
  115. Pomysl *pomysl = first;
  116. while (pomysl != NULL && more < 3) {
  117. for (unsigned i = 0; i<10; ++i) more += (wzor[i]<pomysl->CNT[i]);
  118. if (more >= 3) { // pasuje
  119. cout << pomysl->nazwa << endl; // wyświetla nazwę
  120. for (int i = 0; i < 10; i++) {
  121. wzor[i] = pomysl->CNT[i];
  122. }
  123. // i usuwa:
  124. delete_pomysl(pomysl);
  125. }
  126. else { // znajduje niepasujący
  127. // usuwa:
  128. delete_pomysl(pomysl);
  129. }
  130. pomysl = pomysl->next;
  131. }
  132. }
  133. }
  134.  
  135. int main() {
  136.  
  137. int n; // liczba operacji
  138. int s; // liczba cyfr w opisie startowym
  139. string nazwa; // nazwa pomysłu
  140. unsigned m; // liczba cyfr w opisie pomysłu
  141. int op; // rodzaj operacji
  142.  
  143. Stos *stos = new Stos;
  144.  
  145. cin >> n;
  146.  
  147. unsigned stan[10] = {}; // stan startowy
  148. unsigned v = 0; // pomocniczo el. tablicy
  149. for (cin >> s; s--; ++stan[v]) cin >> v;
  150. for (int i = 0; i < 10; i++) {
  151. stos->wzor[i] = stan[i];
  152. }
  153.  
  154.  
  155. for (int i = 0; i < n; i++) {
  156. cin >> op;
  157. if (op == 1) {
  158. // operacja dodawania pomysłu
  159. cin.ignore();
  160. getline(cin, nazwa);
  161. unsigned CNT[10] = {};
  162. unsigned V = 0; // pomocniczo el. tablicy
  163. for (cin >> m; m--; ++CNT[V]) cin >> V;
  164. // funkcja dodająca
  165. stos->insert_in_stack(nazwa, CNT);
  166. }
  167. else if (op == 2) {
  168. // zmiana kolejności
  169. stos->reverse_stack();
  170. }
  171. else if (op == 3) {
  172. // poszukiwanie pomysłu
  173. stos->find_pomysl();
  174. }
  175. }
  176. /*
  177. for (int i = 0; i < 10; i++) {
  178. cout << stos->first->CNT[i] << endl;
  179. }
  180. cout << "wzor:" << endl;
  181. for (int i = 0; i < 10; i++) {
  182. cout << stos->wzor[i] << endl;
  183. }*/
  184.  
  185. return 0;
  186. }
Time limit exceeded #stdin #stdout 5s 3432KB
stdin
Standard input is empty
stdout
Standard output is empty