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