fork download
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. #include<bits/stdc++.h>
  5. //length, compare, substring finding, delete, insert, replace, reverse
  6. using namespace std;
  7.  
  8. int Length(string s)
  9. {
  10. int len = 0;
  11. for(int i=0;s[i]!='\0';i++)
  12. {
  13. len++;
  14. }
  15. return len;
  16. }
  17. int Compare(string s1, string s2)
  18. {
  19. int lens1 = Length(s1);
  20. int lens2 = Length(s2);
  21.  
  22. if(lens1 == lens2)
  23. {
  24. for(int i=0;i<lens1;i++)
  25. {
  26. if(s1[i] == s2[i])
  27. continue;
  28. else
  29. {
  30. if(s1[i] < s2[i])
  31. {
  32. return 1;
  33. }
  34. else
  35. {
  36. return -1;
  37. }
  38. }
  39. }
  40. return 0;
  41. }
  42. else
  43. {
  44. if(lens1 < lens2)
  45. {
  46. for(int i=0;i<lens1;i++)
  47. {
  48. if(s1[i] == s2[i])
  49. {
  50. continue;
  51. }
  52. else
  53. {
  54. if(s1[i] < s2[i])
  55. {
  56. return 1;
  57. }
  58. else
  59. {
  60. return -1;
  61. }
  62. }
  63. }
  64. return 1;
  65. }
  66. else
  67. {
  68. for(int i=0;i<lens2;i++)
  69. {
  70. if(s1[i] == s2[i])
  71. {
  72. continue;
  73. }
  74. else
  75. {
  76. if(s1[i] < s2[i])
  77. {
  78. return 1;
  79. }
  80. else
  81. {
  82. return -1;
  83. }
  84. }
  85. }
  86. return -1;
  87. }
  88. }
  89. }
  90. string Reverse(string s)
  91. {
  92. string res = "";
  93. int len = Length(s);
  94. for(int i=len-1;i>=0;i--)
  95. {
  96. res = res + s[i];
  97. }
  98. return res;
  99. }
  100.  
  101. vector <int> SubStringFind(string s, string sub)
  102. {
  103. int lens = Length(s);
  104. int lensub = Length(sub);
  105. vector <int> pos;
  106. int paisi = 0;
  107. for(int i=0;i<=lens-lensub;i++)
  108. {
  109. int matched = 0;
  110. for(int j=0;j<lensub;j++)
  111. {
  112. if(sub[j] == s[i+j])
  113. {
  114. matched++;
  115. continue;
  116. }
  117. else
  118. break;
  119. }
  120. if(matched == lensub)
  121. {
  122. pos.push_back(i);
  123. }
  124. }
  125. return pos;
  126. }
  127.  
  128. string Delete(string s, string sub)
  129. {
  130. int lens = Length(s);
  131. int lensub = Length(sub);
  132. vector <int> pos;
  133. pos = SubStringFind(s,sub);
  134. string res = "";
  135. int kototomo = 0;
  136. for(int i=0;i<lens;i++)
  137. {
  138. if(kototomo > pos.size() || i != pos[kototomo])
  139. {
  140. res = res + s[i];
  141. }
  142. else
  143. {
  144. i+= (lensub-1);
  145. kototomo++;
  146. }
  147. }
  148. return res;
  149. }
  150.  
  151. string InsertAtPos(string s, int pos, string sub)
  152. {
  153. string res = "";
  154. int lens = Length(s);
  155. int lensub = Length(sub);
  156. for(int i=0;i<pos;i++)
  157. {
  158. res = res + s[i];
  159. }
  160. res = res + sub;
  161. for(int i=pos;i<lens;i++)
  162. {
  163. res = res + s[i];
  164. }
  165. return res;
  166. }
  167.  
  168. string Replace(string s, string sub1, string sub2)
  169. {
  170. int lens = Length(s);
  171. int lensub1 = Length(sub1);
  172. int lensub2 = Length(sub2);
  173.  
  174. vector <int> pos;
  175. pos = SubStringFind(s,sub1);
  176. string res;
  177. res = Delete(s,sub1);
  178. for(int i=0;i<pos.size();i++)
  179. {
  180. res = InsertAtPos(res,pos[i]+(i*(lensub2-lensub1)),sub2);
  181. }
  182. return res;
  183. }
  184.  
  185. int main()
  186. {
  187. //memset(kothay,100,-1);
  188. vector <int> kothay;
  189. string s,sub1,sub2;
  190. int pos;
  191. cin>>s>>sub1>>sub2;
  192. string res = Replace(s,sub1,sub2);
  193. cout<<res;
  194. return 0;
  195. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty