Source code for submission s1051

bugs.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char bug[2000];
  6. char line[3000000];
  7. char lineo[3000000];
  8. int lineop;
  9. int stack[3000000];
  10. int stackp;
  11.  
  12. void push(int val)
  13. {
  14. stack[stackp++] = val;
  15. }
  16.  
  17. int pop()
  18. {
  19. return stack[--stackp];
  20. }
  21.  
  22. int top()
  23. {
  24. return stack[stackp-1];
  25. }
  26.  
  27. int main(int argc, char **argv)
  28. {
  29. int lines, linel, i, bugl, f;
  30. while (1) {
  31. scanf("%d ", &lines);
  32. if (feof(stdin))
  33. break;
  34. gets(bug);
  35. bugl = strlen(bug);
  36. while (lines--) {
  37. stackp=0;
  38. lineop = 0;
  39. gets(line);
  40. linel=strlen(line);
  41. for (i=0; i<linel; i++) {
  42. lineo[lineop++] = line[i];
  43. f = 0;
  44. if (stackp && bug[top()] == line[i]) {
  45. push(pop()+1);
  46. if (top() == bugl) {
  47. lineop -= bugl;
  48. pop();
  49. }
  50. f = 1;
  51. }
  52. if (bug[0] == line[i]) {
  53. push(1);
  54. f = 1;
  55. }
  56. if (!f) stackp = 0;
  57. }
  58. lineo[lineop] = 0;
  59. printf("%s\n", lineo);
  60. }
  61. }
  62. return 0;
  63. }
  64.