Source code for submission s1399

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. if (start == NULL) return;
  54. while(steps--)
  55. {
  56. if (!actual->prev)
  57. return;
  58. actual = actual->prev;
  59. }
  60. }
  61.  
  62. bool contain(const char * str, int stringlen)
  63. {
  64. if (start == NULL) return false;
  65. node * ptr = actual;
  66. while(stringlen--)
  67. {
  68. if (!ptr || ptr->val != *(str++))
  69. return false;
  70. ptr = ptr->next;
  71. }
  72. return true;
  73. }
  74.  
  75. void del(int size)
  76. {
  77. //print();
  78. node * tosetprev = actual->prev;
  79.  
  80. while(size--)
  81. {
  82. node * tmp = actual;
  83. actual = actual->next;
  84. delete tmp;
  85. }
  86. if (!actual)
  87. {
  88. actual=tosetprev;
  89. if (tosetprev) tosetprev->next = NULL;
  90. }
  91. else
  92. {
  93. actual->prev = tosetprev;
  94. if (tosetprev) tosetprev->next = actual;
  95. }
  96.  
  97. if(!tosetprev) start = actual;
  98.  
  99.  
  100. //print();
  101. }
  102.  
  103. bool notEnd()
  104. {
  105. return actual;
  106. }
  107.  
  108. void gonext()
  109. {
  110. actual = actual->next;
  111. }
  112.  
  113. void print()
  114. {
  115. node * tmp = start;
  116. while(tmp)
  117. {
  118. printf("%c",tmp->val);
  119. tmp = tmp->next;
  120. }
  121. printf("\n");
  122. }
  123. };
  124.  
  125.  
  126. int main ()
  127. {
  128. char * bug = new char[2000001];
  129. int lines, buglen;
  130. char * megarr = new char[2000001];
  131. while ( scanf("%d %s\n", &lines ,bug) == 2 )
  132. {
  133. //cout << "line: " << lines << endl;
  134. buglen = strlen(bug);
  135. while (lines--)
  136. {
  137. cin.getline(megarr,2000000,'\n');
  138. linkedlist ll;
  139. ll.create(megarr);
  140. while(ll.notEnd())
  141. {
  142. if (ll.contain(bug,buglen))
  143. {
  144. ll.del(buglen);
  145. ll.goback(buglen);
  146. }
  147. else
  148. {
  149. ll.gonext();
  150. }
  151. }
  152. ll.print();
  153. }
  154. }
  155.  
  156.  
  157. return 0;
  158. }
  159.  

Diff to submission s1331

bugs.cpp

--- c4.s1331.cteam023.bugs.cpp.0.bugs.cpp
+++ c4.s1399.cteam023.bugs.cpp.0.bugs.cpp
@@ -51,4 +51,5 @@
         void goback(int steps)
         {
+                        if (start == NULL) return;
                 while(steps--)
                 {
@@ -61,4 +62,5 @@
         bool contain(const char * str, int stringlen)
         {
+                if (start == NULL) return false;
                 node * ptr = actual;
                 while(stringlen--)
@@ -94,4 +96,6 @@
                 
                 if(!tosetprev) start = actual;
+                
+
                 //print();      
         }