fork download
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. int Length(string s)
  7. {
  8. int len = 0;
  9. for(int i=0;s[i]!='\0';i++)
  10. {
  11. len++;
  12. }
  13. return len;
  14. }
  15.  
  16. vector <int> SubStringFind(string s, string sub)
  17. {
  18. int lens = Length(s);
  19. int lensub = Length(sub);
  20. vector <int> pos;
  21. int paisi = 0;
  22. for(int i=0;i<=lens-lensub;i++)
  23. {
  24. int matched = 0;
  25. for(int j=0;j<lensub;j++)
  26. {
  27. if(sub[j] == s[i+j])
  28. {
  29. matched++;
  30. continue;
  31. }
  32. else
  33. break;
  34. }
  35. if(matched == lensub)
  36. {
  37. pos.push_back(i);
  38. }
  39. }
  40. return pos;
  41. }
  42. string Delete(string s, string sub)
  43. {
  44. int lens = Length(s);
  45. int lensub = Length(sub);
  46. vector <int> pos;
  47. pos = SubStringFind(s,sub);
  48. string res = "";
  49. int kototomo = 0;
  50. for(int i=0;i<lens;i++)
  51. {
  52. if(kototomo > pos.size() || i != pos[kototomo])
  53. {
  54. res = res + s[i];
  55. }
  56. else
  57. {
  58. i+= (lensub-1);
  59. kototomo++;
  60. }
  61. }
  62. return res;
  63. }
  64. string InsertAtPos(string s, int pos, string sub) {
  65. string res = "";
  66. int lens = Length(s);
  67. int lensub = Length(sub);
  68. for(int i=0;i<pos;i++)
  69. {
  70. res = res + s[i];
  71. }
  72. res = res + sub;
  73. for(int i=pos;i<lens;i++)
  74. {
  75. res = res + s[i];
  76. }
  77. return res;
  78. }
  79. string Replace(string s, string sub1, string sub2) {
  80. int lens = Length(s);
  81. int lensub1 = Length(sub1);
  82. int lensub2 = Length(sub2);
  83. vector <int> pos;
  84. pos = SubStringFind(s,sub1);
  85. string res;
  86. res = Delete(s,sub1);
  87. for(int i=0;i<pos.size();i++)
  88. {
  89. res = InsertAtPos(res,pos[i]+(i*(lensub2-lensub1)),sub2);
  90. }
  91. return res;
  92. }
  93. int main()
  94. {
  95. vector <int> kothay;
  96. string s,sub1,sub2;
  97. int pos;
  98. cin>>s>>sub1>>sub2;
  99. string res = Replace(s,sub1,sub2);
  100. cout<<res;
  101. return 0;
  102. }
Success #stdin #stdout 0.01s 5272KB
stdin
abcdebcd
bcd
pqrst
stdout
apqrstepqrst