fork download
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5.  
  6. string a;
  7. int n;
  8.  
  9.  
  10. int solve(int i, int j, int moves)
  11. {
  12. if(i>=j)
  13. return moves;
  14.  
  15.  
  16.  
  17. if(a[i]==a[j])
  18. return solve(i+1, j-1, moves);
  19. else
  20. return min(min(solve(i+1, j-1, moves+1), solve(i+1, j, moves+1)), solve(i, j-1, moves+1));
  21. }
  22.  
  23.  
  24.  
  25.  
  26. int main()
  27. {
  28. int T;
  29. cin >> T;
  30. while(T--)
  31. {
  32. cin >> a;
  33. n = a.length();
  34.  
  35.  
  36. int ans = solve(0, n-1, 0);
  37.  
  38. cout << ans << "\n";
  39. }
  40. }
Success #stdin #stdout 0.06s 15240KB
stdin
6
tanbirahmed
shahriarmanzoor
monirulhasan
syedmonowarhossain
sadrulhabibchowdhury
mohammadsajjadhossain
stdout
5
7
6
8
8
8