Source code for submission s1331

Go to diff to previous submission

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. if (tosetprev) tosetprev->next = NULL;
  88. }
  89. else
  90. {
  91. actual->prev = tosetprev;
  92. if (tosetprev) tosetprev->next = actual;
  93. }
  94.  
  95. if(!tosetprev) start = actual;
  96. //print();
  97. }
  98.  
  99. bool notEnd()
  100. {
  101. return actual;
  102. }
  103.  
  104. void gonext()
  105. {
  106. actual = actual->next;
  107. }
  108.  
  109. void print()
  110. {
  111. node * tmp = start;
  112. while(tmp)
  113. {
  114. printf("%c",tmp->val);
  115. tmp = tmp->next;
  116. }
  117. printf("\n");
  118. }
  119. };
  120.  
  121.  
  122. int main ()
  123. {
  124. char * bug = new char[2000001];
  125. int lines, buglen;
  126. char * megarr = new char[2000001];
  127. while ( scanf("%d %s\n", &lines ,bug) == 2 )
  128. {
  129. //cout << "line: " << lines << endl;
  130. buglen = strlen(bug);
  131. while (lines--)
  132. {
  133. cin.getline(megarr,2000000,'\n');
  134. linkedlist ll;
  135. ll.create(megarr);
  136. while(ll.notEnd())
  137. {
  138. if (ll.contain(bug,buglen))
  139. {
  140. ll.del(buglen);
  141. ll.goback(buglen);
  142. }
  143. else
  144. {
  145. ll.gonext();
  146. }
  147. }
  148. ll.print();
  149. }
  150. }
  151.  
  152.  
  153. return 0;
  154. }
  155.  

Diff to submission s1319

bugs.cpp

--- c4.s1319.cteam023.bugs.cpp.0.bugs.cpp
+++ c4.s1331.cteam023.bugs.cpp.0.bugs.cpp
@@ -85,11 +85,13 @@
                 {
                         actual=tosetprev;
-                        tosetprev->next = NULL;
+                        if (tosetprev) tosetprev->next = NULL;
                 }
                 else
                 {
                         actual->prev = tosetprev;
-                        tosetprev->next = actual;
+                        if (tosetprev) tosetprev->next = actual;
                 }
+                
+                if(!tosetprev) start = actual;
                 //print();      
         }