Source code for submission s1319

bugs.cpp

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <string.h>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. class node
  10. {
  11. public:
  12. node * prev;
  13. node * next;
  14. char val;
  15. node(char a)
  16. {
  17. prev = next = NULL;
  18. val = a;
  19. }
  20. };
  21.  
  22. class linkedlist
  23. {
  24. public:
  25. node * start;
  26. node * end;
  27. node * actual;
  28.  
  29. void create(const char * arr)
  30. {
  31. start = end = actual = NULL;
  32. node * tmp = NULL;
  33. node * old = NULL;
  34. while(*arr)
  35. {
  36. tmp = new node(*arr);
  37. tmp->prev = old;
  38. if (old)
  39. old->next = tmp;
  40. else
  41. start = tmp;
  42. old = tmp; //printf("%s\n", megarr);
  43. arr++;
  44. }
  45. if (tmp)
  46. tmp->next = NULL;
  47. end = tmp;
  48. actual = start;
  49. }
  50.  
  51. void goback(int steps)
  52. {
  53. while(steps--)
  54. {
  55. if (!actual->prev)
  56. return;
  57. actual = actual->prev;
  58. }
  59. }
  60.  
  61. bool contain(const char * str, int stringlen)
  62. {
  63. node * ptr = actual;
  64. while(stringlen--)
  65. {
  66. if (!ptr || ptr->val != *(str++))
  67. return false;
  68. ptr = ptr->next;
  69. }
  70. return true;
  71. }
  72.  
  73. void del(int size)
  74. {
  75. //print();
  76. node * tosetprev = actual->prev;
  77.  
  78. while(size--)
  79. {
  80. node * tmp = actual;
  81. actual = actual->next;
  82. delete tmp;
  83. }
  84. if (!actual)
  85. {
  86. actual=tosetprev;
  87. tosetprev->next = NULL;
  88. }
  89. else
  90. {
  91. actual->prev = tosetprev;
  92. tosetprev->next = actual;
  93. }
  94. //print();
  95. }
  96.  
  97. bool notEnd()
  98. {
  99. return actual;
  100. }
  101.  
  102. void gonext()
  103. {
  104. actual = actual->next;
  105. }
  106.  
  107. void print()
  108. {
  109. node * tmp = start;
  110. while(tmp)
  111. {
  112. printf("%c",tmp->val);
  113. tmp = tmp->next;
  114. }
  115. printf("\n");
  116. }
  117. };
  118.  
  119.  
  120. int main ()
  121. {
  122. char * bug = new char[2000001];
  123. int lines, buglen;
  124. char * megarr = new char[2000001];
  125. while ( scanf("%d %s\n", &lines ,bug) == 2 )
  126. {
  127. //cout << "line: " << lines << endl;
  128. buglen = strlen(bug);
  129. while (lines--)
  130. {
  131. cin.getline(megarr,2000000,'\n');
  132. linkedlist ll;
  133. ll.create(megarr);
  134. while(ll.notEnd())
  135. {
  136. if (ll.contain(bug,buglen))
  137. {
  138. ll.del(buglen);
  139. ll.goback(buglen);
  140. }
  141. else
  142. {
  143. ll.gonext();
  144. }
  145. }
  146. ll.print();
  147. }
  148. }
  149.  
  150.  
  151. return 0;
  152. }
  153.