Source code for submission s1204

Go to diff to previous submission

bugs.cpp

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. int main ( )
  9. {
  10. int lines;
  11. string expr;
  12. while ( cin >> lines >> expr ) {
  13.  
  14. cin . get();
  15. int exprL = ( int ) expr . length();
  16. int array[ 2000001 ];
  17. while ( lines-- )
  18. {
  19. int cnt = 0;
  20. char ch;
  21. //start = end = array;
  22. //getline( cin, line );
  23. int curr = 0;
  24. //memset( array, 0, sizeof( array ) );
  25. while ( (ch = cin.get()) != '\n' )
  26. {
  27. if ( ! cin . good() )
  28. {
  29. ch = '\0';
  30. break;
  31. }
  32. if(ch == expr[curr] && curr > 0) curr++;
  33. else if(ch == expr[0]) {
  34. if(curr > 0) array[cnt++] = curr;
  35. curr = 1;
  36. }
  37. else {
  38. if(cnt > 0) {
  39. for(int i = 0; i < cnt; i++) cout << expr.substr(0, array[i]);
  40. cnt = 0;
  41. }//vypiš zásobník
  42. if(curr > 0) {
  43. cout << expr.substr(0, curr);
  44. }//vypiš current
  45. cout << ch;
  46. cnt = 0;
  47. curr = 0;
  48. }
  49.  
  50. if ( curr == exprL ) {
  51. if(cnt > 0) curr = array[--cnt];
  52. else curr = 0;
  53. }//načti předchozí ze zásobníku
  54.  
  55.  
  56. }
  57. if(cnt > 0) {
  58. for(int i = 0; i < cnt; i++) cout << expr.substr(0, array[i]);
  59. cnt = 0;
  60. }//vypiš zásobník
  61. if(curr > 0) {
  62. cout << expr.substr(0, curr);
  63. }//vypiš current
  64. if ( ch == '\n' ) cout << endl;
  65. //printuj!
  66. }
  67. }
  68. return 0;
  69. }
  70.  

Diff to submission s1033

bugs.cpp

--- c4.s1033.cteam011.bugs.cpp.0.bugs.cpp
+++ c4.s1204.cteam011.bugs.cpp.0.bugs.cpp
@@ -11,6 +11,5 @@
         string expr;
         while ( cin >> lines >> expr ) {
-         
-        
+                
         cin . get();
         int exprL = ( int ) expr . length();
@@ -23,7 +22,12 @@
                 //getline( cin, line );
                 int curr = 0;
-                memset( array, 0, sizeof( array ) );
+                //memset( array, 0, sizeof( array ) );
                 while ( (ch = cin.get()) != '\n' )
                 {
+                        if ( ! cin . good() ) 
+                        {
+                                ch = '\0';
+                                break;
+                        }
                         if(ch == expr[curr] && curr > 0) curr++;
                         else if(ch == expr[0]) {
@@ -58,5 +62,5 @@
                         cout << expr.substr(0, curr);
                 }//vypiš current
-                cout << endl;
+                if ( ch == '\n' ) cout << endl;
                 //printuj!
                 }