fork(1) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4.  
  5. //function to insert element after vTh postion
  6. void cp(char * c,char p,ll v,ll tt)
  7. {
  8. ll z,x;z=v;char cm=p,cn;
  9. for(x=z;x<tt;x++)
  10. {
  11. cn = c[x];
  12. c[x] = cm;
  13. cm =cn;
  14. }
  15. }
  16. //function to insert element after vTh postion
  17.  
  18. int main(){
  19. ll a,b,c,d,e,f,g,ii,mm,nn,bb,vv,cc,xx,zz,ff;
  20. cin>>ff;
  21. for(g=0;g<ff;g++)
  22. {
  23. cin>>a>>b;
  24. char arr[a+1],arr2[b+1],arr3[a+b+1],arr4[a+b+1];
  25. fflush(stdin);//cin.ignore();
  26. scanf("%s",&arr);
  27. fflush(stdin);
  28. scanf("%s",&arr2);
  29.  
  30. fflush(stdin);
  31.  
  32. d=0;c=0;
  33. strcpy(arr3,arr);
  34. strcpy(arr4,arr2);
  35.  
  36. nn = a;
  37. vv=0;
  38.  
  39. /* technique
  40.  for each element of str2,finding first element in str1 which is equal to element of str2
  41.  and inserting it there(say at position 'p') then for next element of str2 finding first element in str1 starting from p
  42.   */
  43. //first case
  44. for(bb=0;bb<b;bb++)
  45. {
  46. xx=0;
  47.  
  48. for(mm=vv;mm<nn;mm++)
  49. {
  50. if(arr3[mm]==arr2[bb])
  51. {
  52. cp(arr3,arr2[bb],mm+1,nn+1);vv = mm+1;nn++;xx++;break;
  53. }
  54. }
  55. if(xx==0)
  56. {
  57. cp(arr3,arr2[bb],mm,nn+1);vv = mm;nn++;
  58. }
  59. }
  60. //first case
  61.  
  62. ll nnn;
  63. vv=0;nnn=b;
  64.  
  65. //second case
  66. for(bb=0;bb<a;bb++)
  67. {xx=0;
  68. for(mm=vv;mm<nnn;mm++)
  69. { if(arr4[mm]==arr[bb])
  70. {
  71. cp(arr4,arr[bb],mm+1,nnn+1);vv = mm+1;nnn++;xx++;break;
  72. }
  73. }
  74. if(xx==0)
  75. {
  76. cp(arr4,arr[bb],mm,nnn+1);vv = mm;nnn++;
  77. }
  78. }
  79.  
  80. //second case
  81.  
  82. //for both case finding ans
  83.  
  84. char hh=arr3[0];ll bb=1;
  85.  
  86. for(f=1;f<nn;f++)
  87. {
  88. if(arr3[f]!=arr3[f-1])bb++;
  89. }
  90.  
  91. char hhh=arr4[0];ll bbb=1;
  92. for(f=1;f<nnn;f++)
  93. {
  94. if(arr4[f]!=arr4[f-1])bbb++;
  95. }
  96.  
  97. //for both case finding ans
  98.  
  99. cout<<min(bb,bbb)<<"\n";
  100. //taking min
  101. }}
  102.  
Runtime error #stdin #stdout 0s 4396KB
stdin
Standard input is empty
stdout
Standard output is empty