Source code for submission s1124

Go to diff to previous submission

bugs.cpp

  1. //
  2. // File: mosquito.cc
  3. // Author: cteam035
  4. //
  5. // Created on October 27, 2012, 11:23 AM
  6. //
  7.  
  8. #include <cstdlib>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <string>
  12. #include <string>
  13. #include <iostream>
  14.  
  15.  
  16.  
  17. using namespace std;
  18. //
  19. //
  20. //
  21.  
  22. #define MAXCHAR 2000002
  23. #define MAXSTR 1005
  24.  
  25. class Stack{
  26. public:
  27. Stack();
  28. ~Stack();
  29. void setRet(const string & str);
  30. // top -- jaky potrebuju
  31. bool top(char& c, int& i) const;
  32. char first(void) const;
  33. bool push(char c, int i);
  34. void check();
  35. void vysypej();
  36. bool isEmpty(void) const;
  37.  
  38. private:
  39. char poleC[MAXCHAR];
  40. int poleI[MAXCHAR];
  41. int vrchol;
  42. char retezec[MAXSTR];
  43. int delkaRet;
  44. };
  45.  
  46. Stack::Stack() {
  47. vrchol = 0;
  48. retezec[0]='\0';
  49. delkaRet=0;
  50. }
  51.  
  52. Stack::~Stack() {
  53.  
  54. }
  55.  
  56. bool Stack::isEmpty(void) const {
  57. return vrchol == 0;
  58. }
  59.  
  60. void Stack::setRet(const string & str){
  61. strncpy(retezec, str.c_str(), MAXSTR);
  62. delkaRet=str.length();
  63. }
  64.  
  65. char Stack::first() const {
  66. return retezec[0];
  67. }
  68.  
  69. bool Stack::top(char& c, int& i) const {
  70. if (isEmpty())
  71. i=0;
  72. else
  73. i = poleI[vrchol-1]+1;
  74. c = retezec[i];
  75. return true;
  76. }
  77.  
  78. bool Stack::push(char c, int i) {
  79. if ( vrchol >= MAXCHAR )
  80. return false;
  81. poleC[vrchol]=c;
  82. poleI[vrchol]=i;
  83. vrchol++;
  84. return false;
  85. }
  86.  
  87. void Stack::check(){
  88. int i;
  89. for ( i=0; i<delkaRet; i++) {
  90. if ( poleC[vrchol-1-i] != retezec[delkaRet-1-i] )
  91. return;
  92. }
  93. vrchol-=delkaRet;
  94.  
  95. }
  96.  
  97. void Stack::vysypej(){
  98. for ( int i=0; i<vrchol; i++ ){
  99. fputc(poleC[i],stdout);
  100. }
  101. vrchol=0;
  102. }
  103.  
  104. int main(int argc, char** argv) {
  105. Stack* stack = new Stack();
  106. int nlines, n, index;
  107. char c, expect, prvni;
  108. string bug;
  109.  
  110. while ( cin >> n >> bug ) {
  111. fgetc(stdin);
  112. stack->setRet(bug);
  113.  
  114. nlines=0;
  115. c='\0';
  116. while ( nlines < n ) {
  117. c = fgetc(stdin);
  118. if ( c == '\n' ) {
  119. nlines++;
  120. stack->vysypej();
  121. fputc(c,stdout);
  122. continue;
  123. }
  124.  
  125. prvni = stack->first();
  126. stack->top(expect, index);
  127. if ( expect == c ) {
  128. stack->push(c, index);
  129. stack->check();
  130. }
  131. else if ( prvni == c ){
  132. stack->push(c, 0);
  133. stack->check();
  134. }
  135. else {
  136. stack->vysypej();
  137. fputc(c,stdout);
  138. }
  139. }
  140. }
  141.  
  142. return (EXIT_SUCCESS);
  143. }
  144.  
  145.  

Diff to submission s1114

bugs.cpp

--- c4.s1114.cteam035.bugs.cpp.0.bugs.cpp
+++ c4.s1124.cteam035.bugs.cpp.0.bugs.cpp
@@ -97,6 +97,5 @@
 void Stack::vysypej(){
     for ( int i=0; i<vrchol; i++ ){
-        cout << poleC[i];
-        cout.flush();
+        fputc(poleC[i],stdout);
     }
     vrchol=0;
@@ -110,5 +109,5 @@
     
     while ( cin >> n >> bug ) {
-        cin.get();
+        fgetc(stdin);
         stack->setRet(bug);
         
@@ -116,10 +115,9 @@
         c='\0';
         while ( nlines < n ) {
-            c = cin.get();
+            c = fgetc(stdin);
             if ( c == '\n' ) {
                 nlines++;
                 stack->vysypej();
-                cout << c;
-                cout.flush();
+                fputc(c,stdout);
                 continue;
             }
@@ -137,6 +135,5 @@
             else {
                 stack->vysypej();
-                cout << c;
-                cout.flush();
+                fputc(c,stdout);
             }
         }