Source code for submission s1088

bugs.cpp

  1. //
  2. // File: bugs.cc
  3. // Author: cteam049
  4. //
  5. // Created on October 27, 2012, 12:03 PM
  6. //
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <iostream>
  12.  
  13. using namespace std;
  14.  
  15. //
  16. //
  17. //
  18.  
  19. bool findBug(int index, int lineLength, char* line, char* replacement, int replLength){
  20. for(int i = index; i - index < replLength ; i++) {
  21. if(i >= lineLength) return false;
  22. if(line[i] != replacement[i - index]) return false;
  23. }
  24. //cout << "bug nalezen" << endl;
  25. return true;
  26. }
  27.  
  28. int main(int argc, char** argv) {
  29. int lines, len, pos, lineLength = 2000002, tmpLength = 2000002, replLength = 1001;
  30. char line[2000002];
  31. char lineTmp[2000002];
  32. char replacement[1001];
  33. char tmp;
  34. bool found;
  35.  
  36. while(1){
  37. if(scanf("%d", &lines) != 1) break;
  38. if(scanf(" %s", &replacement) != 1) break;
  39. //precti cislo
  40. //scanf("%d %s", &lines, &replacement);
  41. replLength = strlen(replacement);
  42. getchar();
  43.  
  44. // cout << "\nlines je " << lines << endl;
  45. // cout << "replacement je " << replacement << endl;
  46. // cout << "delka replacementu je " << replLength << endl;
  47.  
  48. for(int i = 0; i < lines; i++) {
  49. tmpLength = 0;
  50. lineLength = 0;
  51. //nacti radek
  52. do {
  53. tmp = getchar();
  54. lineTmp[tmpLength] = tmp;
  55. tmpLength++;
  56. } while (tmp != '\n');
  57. // cout << "delka radku je " << tmpLength << endl;
  58.  
  59. found = true;
  60. //ted je radek nacten
  61. while(found) {
  62. found = false;
  63. lineLength = 0;
  64.  
  65. for(int j = 0; j < tmpLength; j++) {
  66. //hledame vyskyty bugu
  67. if(findBug(j, tmpLength, lineTmp, replacement, replLength)){
  68. j+= replLength - 1;
  69. found = true;
  70. }
  71. else {
  72. line[lineLength] = lineTmp[j];
  73. lineLength++;
  74. }
  75. //cout << "i je " << i << endl;
  76.  
  77.  
  78. }
  79. // cout << "radek po pruchodu: " << endl;
  80. // bugs.cc:100: error: expected unqualified-id before 'return' for(int i = 0; i < lineLength; i++) {
  81. // cout << line[i];
  82. // }
  83. // cout << endl;
  84.  
  85.  
  86. for(int j = 0; j < lineLength; j++) {
  87. lineTmp[j] = line[j];
  88. }
  89. tmpLength = lineLength;
  90. }
  91. for(int k = 0; k < lineLength; k++) {
  92. //cout << line[k];
  93. putchar(line[k]);
  94. }
  95. //cout << endl;
  96. }
  97. }
  98. return 0;
  99. }
  100.  
  101.