Source code for submission s1114

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. cout << poleC[i];
  100. cout.flush();
  101. }
  102. vrchol=0;
  103. }
  104.  
  105. int main(int argc, char** argv) {
  106. Stack* stack = new Stack();
  107. int nlines, n, index;
  108. char c, expect, prvni;
  109. string bug;
  110.  
  111. while ( cin >> n >> bug ) {
  112. cin.get();
  113. stack->setRet(bug);
  114.  
  115. nlines=0;
  116. c='\0';
  117. while ( nlines < n ) {
  118. c = cin.get();
  119. if ( c == '\n' ) {
  120. nlines++;
  121. stack->vysypej();
  122. cout << c;
  123. cout.flush();
  124. continue;
  125. }
  126.  
  127. prvni = stack->first();
  128. stack->top(expect, index);
  129. if ( expect == c ) {
  130. stack->push(c, index);
  131. stack->check();
  132. }
  133. else if ( prvni == c ){
  134. stack->push(c, 0);
  135. stack->check();
  136. }
  137. else {
  138. stack->vysypej();
  139. cout << c;
  140. cout.flush();
  141. }
  142. }
  143. }
  144.  
  145. return (EXIT_SUCCESS);
  146. }
  147.  
  148.