fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool check(int a[], int b[], int i, int j, int n)
  5. {
  6. int count = 0;
  7. int y = i;
  8. int c = 1;
  9.  
  10. while(y <= j && c <= n)
  11. {
  12. if(a[y] == b[c])
  13. {
  14. count = count + 1;
  15. y = y + 1;
  16. c = c + 1;
  17. }
  18. else
  19. {
  20. c = c + 1;
  21. }
  22. }
  23.  
  24. if(count == j - i + 1)
  25. return true;
  26.  
  27. return false;
  28. }
  29.  
  30. int main()
  31. {
  32. int n;
  33. cin >> n;
  34.  
  35. int a[n+1] = {0};
  36. int b[n+1] = {0};
  37.  
  38. for(int i=1; i<=n; i++)
  39. {
  40. cin >> a[i];
  41. }
  42.  
  43. for(int i=1; i<=n; i++)
  44. {
  45. cin >> b[i];
  46. }
  47.  
  48. int r = 0;
  49.  
  50. for(int i=1; i<=n; i++)
  51. {
  52. for(int j=i; j<=n; j++)
  53. {
  54. // a[i...j]
  55.  
  56. if(check(a,b,i,j,n) == true)
  57. {
  58. int length = j - i + 1;
  59. r = max(r, length);
  60. }
  61. }
  62. }
  63.  
  64. cout <<"Number of Operations needed to make Array 'B' equal Array 'A: "<<n-r;
  65. }
Success #stdin #stdout 0s 5328KB
stdin
5
1 2 3 4 5 
2 3 4 5 1
stdout
Number of Operations needed to make Array 'B' equal Array 'A: 1