Source code for submission s1151

Go to diff to previous submission

bugs.cpp

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <string>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. static int pi[1100];
  10.  
  11. /*void popn(string &s, int n) {
  12.   for(int i = 0; i < n; i++)
  13.   s.pop();
  14. }*/
  15.  
  16. void compute_prefix(string &P) {
  17. int m = P.length() - 1;
  18. pi[1] = 0;
  19. int k = 0;
  20.  
  21. for(int q=2; q <= m; q++) {
  22. while(k > 0 && P[k+1] != P[q]) {
  23. k = pi[k];
  24. }
  25. if(P[k+1] == P[q]) {
  26. k = k+1;
  27. }
  28. pi[q] = k;
  29. }
  30. }
  31.  
  32. void kmp_matcher(string &T, string &P) {
  33. int n = T.length() - 1;
  34. int m = P.length() - 1;
  35.  
  36. int q = 0;
  37. for (int i = 1; i <= n; i++) {
  38. while(q > 0 && P[q + 1] != T[i])
  39. {
  40. q = pi[q];
  41. }
  42. if(P[q + 1] == T[i])
  43. {
  44. q++;
  45. }
  46. if(q == m)
  47. {
  48. //cerr << i << " " << m << endl;
  49. T.erase(i - m, m);
  50. //cerr << T << endl;
  51. n -= m;
  52. i -= 2*m;
  53. q = pi[q];
  54. if (i < 1)
  55. {
  56. i = 0;
  57. q = 0;
  58. }
  59. }
  60. }
  61. }
  62.  
  63. int main()
  64. {
  65. uint cases;
  66.  
  67.  
  68. string pat;
  69. while (cin >> cases >> pat)
  70. {
  71. pat = " " + pat;
  72. getchar();
  73. for (uint c = 0; c < cases; ++c)
  74. {
  75. string tex;
  76. getline(cin, tex);
  77. tex = " " + tex;
  78.  
  79. kmp_matcher(tex, pat);
  80.  
  81. printf("%s\n", tex.c_str() + 1);
  82. }
  83. }
  84. return 0;
  85. }
  86.  
  87. /*
  88.  *
  89.  * void compute_prefix(string &P) {
  90.   int m = P.length();
  91.   pi[1] = 0;
  92.   int k = 0;
  93.  
  94.   for(int q=2; q <= m; q++) {
  95.   while(k > 0 && P[k] != P[q-1]) {
  96.   k = pi[k];
  97.   }
  98.   if(P[k] == P[q-1]) {
  99.   k = k+1;
  100.   }
  101.   pi[q] = k;
  102.   }
  103. }
  104. void kmp_matcher(string &T, string &P) {
  105.   int n = T.length();
  106.   int m = P.length();
  107.  
  108.   int q = 0;
  109.   for (int i = 1; i <= n; i++) {
  110.   while(q > 0 && P[q] != T[i-1]) {
  111.   q = pi[q];
  112.   }
  113.   if(P[q] == T[i-1]) {
  114.   q++;
  115.   }
  116.   if(q ==m ) {
  117.   //cerr << i << " " << m << endl;
  118.   T.erase(i-m, m);
  119.   //cerr << T << endl;
  120.   n = n-m;
  121.   i -= 2*m-1;
  122.   q = pi[q];
  123.   if (i < 1) {
  124.   i = 0;
  125.   q = 0;
  126.   }
  127.   }
  128.   }
  129. }
  130. */

Diff to submission s1045

bugs.cpp

--- c4.s1045.cteam097.bugs.cpp.0.bugs.cpp
+++ c4.s1151.cteam097.bugs.cpp.0.bugs.cpp
@@ -15,13 +15,13 @@
 
 void compute_prefix(string &P) {
-    int m = P.length();
+    int m = P.length() - 1;
     pi[1] = 0;
     int k = 0;
     
     for(int q=2; q <= m; q++) {
-        while(k > 0 && P[k] != P[q-1]) {
+        while(k > 0 && P[k+1] != P[q]) {
             k = pi[k];
         }
-        if(P[k] == P[q-1]) {
+        if(P[k+1] == P[q]) {
             k = k+1;
         }
@@ -31,23 +31,27 @@
 
 void kmp_matcher(string &T, string &P) {
-    int n = T.length();
-    int m = P.length();
+    int n = T.length() - 1;
+    int m = P.length() - 1;
     
     int q = 0;
     for (int i = 1; i <= n; i++) {
-        while(q > 0 && P[q] != T[i-1]) {
+        while(q > 0 && P[q + 1] != T[i])
+        {
             q = pi[q];
         }
-        if(P[q] == T[i-1]) {
+        if(P[q + 1] == T[i])
+        {
             q++;
         }
-        if(q ==m ) {
+        if(q == m)
+        {
             //cerr << i << " " << m << endl;
-            T.erase(i-m, m);
+            T.erase(i - m, m);
             //cerr << T << endl;
-            n = n-m;
-            i -= 2*m-1;
+            n -= m;
+            i -= 2*m;
             q = pi[q];
-            if (i < 1) {
+            if (i < 1)
+            {
                 i = 0;
                 q = 0;
@@ -59,19 +63,68 @@
 int main()
 {
-    string pat;
     uint cases;
-    string text;
+    
+    
+    string pat;
     while (cin >> cases >> pat)
     {
+        pat = " " + pat;
         getchar();
         for (uint c = 0; c < cases; ++c)
         {
-            text = "";
-            getline(cin, text);
-            kmp_matcher(text, pat);
+            string tex;
+            getline(cin, tex);
+            tex = " " + tex;
             
-            printf("%s\n", text.c_str());
+            kmp_matcher(tex, pat);
+            
+            printf("%s\n", tex.c_str() + 1);
         }
     }
     return 0;
-}
\ No newline at end of file
+}
+
+/*
+ * 
+ * void compute_prefix(string &P) {
+    int m = P.length();
+    pi[1] = 0;
+    int k = 0;
+    
+    for(int q=2; q <= m; q++) {
+        while(k > 0 && P[k] != P[q-1]) {
+            k = pi[k];
+        }
+        if(P[k] == P[q-1]) {
+            k = k+1;
+        }
+        pi[q] = k;
+    }
+}
+void kmp_matcher(string &T, string &P) {
+    int n = T.length();
+    int m = P.length();
+    
+    int q = 0;
+    for (int i = 1; i <= n; i++) {
+        while(q > 0 && P[q] != T[i-1]) {
+            q = pi[q];
+        }
+        if(P[q] == T[i-1]) {
+            q++;
+        }
+        if(q ==m ) {
+            //cerr << i << " " << m << endl;
+            T.erase(i-m, m);
+            //cerr << T << endl;
+            n = n-m;
+            i -= 2*m-1;
+            q = pi[q];
+            if (i < 1) {
+                i = 0;
+                q = 0;
+            }
+        }
+    }
+}
+*/
\ No newline at end of file