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