Source code for submission s995

bugs.cpp

  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int T;
  7. string bugText;
  8. int lineEnds;
  9.  
  10. bool bugComplete(string text)
  11. {
  12. if (text.size() != bugText.size())
  13. return false;
  14.  
  15. int i = text.size() -1;
  16. return text[i] == bugText[i];
  17. //return text == bugText;
  18. }
  19.  
  20. bool bugStart(char ch)
  21. {
  22. if (ch == bugText[0])
  23. return true;
  24.  
  25. return false;
  26. }
  27.  
  28. bool bugFailed(string text)
  29. {
  30. if (text.size() > bugText.size())
  31. return true;
  32.  
  33. for (int i = text.size() -1 ; i >= 0; --i)
  34. {
  35. if (text[i] != bugText[i])
  36. return true;
  37. }
  38.  
  39. return false;
  40. }
  41.  
  42. string readBug()
  43. {
  44. string text;
  45. while (lineEnds < T)
  46. {
  47. char ch;
  48. ch = cin.get();
  49. if (ch == '\n')
  50. {
  51. //cout << "endl" << endl;
  52. lineEnds++;
  53. text += ch;
  54. continue;
  55. }
  56.  
  57. if (bugStart(ch))
  58. {
  59. //cout << "bugstart" << endl;
  60. if (text.empty())
  61. text += ch;
  62. else
  63. {
  64. cin.unget();
  65. text += readBug();
  66. }
  67. }
  68. else
  69. {
  70. //cout << "check bug" << endl;
  71. text += ch;
  72.  
  73. if (bugComplete(text))
  74. {
  75. //cout << "bug complete!!" << endl;
  76. return "";
  77. }
  78.  
  79. if (bugFailed(text))
  80. return text;
  81. }
  82.  
  83. }
  84. return text;
  85. }
  86.  
  87. string baseLoop()
  88. {
  89. string text;
  90. while (lineEnds < T)
  91. {
  92. char ch;
  93. ch = cin.get();
  94.  
  95. if (ch == '\n')
  96. {
  97. // cout << "endl" << endl;
  98. lineEnds++;
  99. text += ch;
  100. continue;
  101. }
  102.  
  103. if (bugStart(ch))
  104. {
  105. //cout << "bugstart" << endl;
  106. cin.unget();
  107. text += readBug();
  108. }
  109. else
  110. text += ch;
  111. }
  112.  
  113. return text;
  114. }
  115.  
  116. int main()
  117. {
  118.  
  119. while (true)
  120. {
  121. cin >> T;
  122. cin >> bugText;
  123. if (!cin.good())
  124. {
  125. break;
  126. }
  127.  
  128. lineEnds = 0;
  129. char ch;
  130. ch = cin.get();
  131. if (ch != '\n')
  132. cin.unget();
  133. cout << baseLoop() << endl;
  134. }
  135.  
  136. }