fork download
  1. /* Coding is today's language of creativity
  2. ----------------I am NIKHIL MULCHANDANI-----------------
  3.   SARVAJANIK COLLEGE OF ENGINEERING & TECHNOLOGY,SURAT
  4.   B.E COMPUTER ENGINEERING 2ND YEAR
  5. */
  6. #include <iostream>
  7. #include<stdio.h>
  8. #include<string.h>
  9. #include<math.h>
  10. #include<stdlib.h>
  11. #include<vector>
  12. #include<string>
  13. #include<algorithm>
  14. #include<map>
  15. #include<set>
  16. #include<stack>
  17. #include<list>
  18. #include<ctype.h>
  19. using namespace std;
  20. #define array_input(a,n) for(i=0;i<n;i++)cin>>a[i];
  21. #define array_output(a,n) for(i=0;i<n;i++)cout<<a[i]<<" ";
  22.  
  23. int main()
  24. {
  25. /* int t;
  26.   cin>>t;
  27.   while (t--) {
  28.   /* code */
  29. //int count[62];
  30. int n,i,j;
  31. scanf("%d",&n);
  32. int l[n+1][n+1];
  33. string s,ss;
  34. cin>>s;
  35. ss=s;
  36. reverse(ss.begin(),ss.end());
  37. for ( i = 0; i <=n; i++) {
  38. /* code */
  39. for ( j = 0; j <=n; j++) {
  40. /* code */
  41. if(i==0||j==0)l[i][j]=0;
  42. else if(s[i-1]==ss[j-1])l[i][j]=1+l[i-1][j-1];
  43. else
  44. {
  45. l[i][j]=max(l[i-1][j],l[i][j-1]);
  46. }
  47. }
  48. }
  49. std::cout << n-l[n][n]<<"\n";
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 15240KB
stdin
10
abcddcbaeg
stdout
2