Advertisement
Guest User

Untitled

a guest
Oct 4th, 2019
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6.  
  7. class circularList
  8. {
  9. public:
  10. circularList(int _data = 0) : data(_data), next(this), prev(this) {}
  11. circularList(int _data, circularList* _next, circularList* _prev) : data(_data), next(_next), prev(_prev) {}
  12.  
  13. void addNode(int data)
  14. {
  15. circularList* newNode = new circularList(data, this->next, this);
  16.  
  17. this->next->prev = newNode;
  18. this->next = newNode;
  19. }
  20.  
  21. void deleteNode()
  22. {
  23. this->prev->next = this->next;
  24. this->next->prev = this->prev;
  25.  
  26. delete this;
  27. }
  28.  
  29. public:
  30. int data;
  31. circularList* next;
  32. circularList* prev;
  33. };
  34.  
  35.  
  36. void computeArray(const std::string& substring, std::vector<int>& arr)
  37. {
  38. int i = 0;
  39. int j = 1;
  40.  
  41. while (j < substring.length())
  42. {
  43. if (substring[i] == substring[j])
  44. {
  45. i++;
  46. arr[j] = i;
  47. j++;
  48. }
  49. else
  50. {
  51. if (i != 0)
  52. {
  53. i = arr[i - 1];
  54. }
  55. else
  56. {
  57. arr[j] = 0;
  58. j++;
  59. }
  60. }
  61. }
  62. }
  63.  
  64.  
  65. bool KMP(const std::string& recipeScores, int& index, const std::vector<int>& arr, int score)
  66. {
  67. if (recipeScores[index] - '0' == score)
  68. {
  69. index++;
  70. if (index == recipeScores.length())
  71. {
  72. return true;
  73. }
  74. }
  75. else
  76. {
  77. while (index != 0)
  78. {
  79. index = arr[index - 1];
  80.  
  81. if (recipeScores[index] - '0' == score)
  82. {
  83. index++;
  84. return false;
  85. }
  86. }
  87. }
  88.  
  89. return false;
  90. }
  91.  
  92.  
  93. int main()
  94. {
  95. std::fstream in("input.in", std::fstream::in);
  96. std::fstream out("output.out", std::fstream::out);
  97.  
  98. const int firstRecipeScore = 3;
  99. const int secondRecipeScore = 7;
  100. std::string substring;
  101. in >> substring;
  102.  
  103. // Auxiliar vector needed for the KMP algorithm
  104. std::vector<int> arr(substring.length());
  105. computeArray(substring, arr);
  106.  
  107. // Creating the circular list
  108. circularList* last = new circularList(firstRecipeScore);
  109. last->addNode(secondRecipeScore);
  110. last = last->next;
  111.  
  112. circularList* firstElf = last->prev;
  113. circularList* secondElf = last;
  114. int listLength = 2;
  115. int sum = 0;
  116. int min = 0, max = 0;
  117.  
  118. int index = 0;
  119.  
  120. while (true)
  121. {
  122. // Calculating the sum, adding the sum to the end of the list and checking if a matching was found
  123. sum = firstElf->data + secondElf->data % 10;
  124.  
  125. if (sum > 9)
  126. {
  127. last->addNode(1);
  128. last = last->next;
  129. listLength++;
  130.  
  131. if (KMP(substring, index, arr, 1) == true)
  132. {
  133. break;
  134. }
  135. }
  136. last->addNode(sum % 10);
  137. last = last->next;
  138. listLength++;
  139.  
  140. if (KMP(substring, index, arr, sum % 10) == true)
  141. {
  142. break;
  143. }
  144.  
  145. // Calculating the new positions for the first elf and second elf
  146. if (firstElf->data < secondElf->data)
  147. {
  148. min = firstElf->data;
  149. max = secondElf->data;
  150.  
  151. for (int i = 0; i < max - min; i++)
  152. {
  153. secondElf = secondElf->next;
  154. }
  155. }
  156. else
  157. {
  158. min = secondElf->data;
  159. max = firstElf->data;
  160.  
  161. for (int i = 0; i < max - min; i++)
  162. {
  163. firstElf = firstElf->next;
  164. }
  165. }
  166.  
  167. for (int i = 0; i <= min; i++)
  168. {
  169. firstElf = firstElf->next;
  170. secondElf = secondElf->next;
  171. }
  172. }
  173.  
  174. out << listLength - substring.length();
  175.  
  176. in.close();
  177. out.close();
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement