spider68

longest_palindrome_subseq

Mar 15th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int test(string s1,string s2)
  5. {
  6. int i,j;
  7. int n=s1.length();
  8. int a[n+1][n+1];
  9. for(i=0;i<=n;i++)
  10. for(j=0;j<=n;j++)
  11. {
  12. if(i==0||j==0){a[i][j]=0; continue;}
  13. if(s1[i-1]==s2[j-1])a[i][j]=1+a[i-1][j-1];
  14. else a[i][j]=max(a[i-1][j],a[i][j-1]);
  15. }
  16. return a[n][n];
  17. }
  18. int main()
  19. {
  20. int i,j,m,n,t;
  21. cin>>t;
  22. while(t--)
  23. {
  24. string s1,s2;
  25. cin>>s1;
  26. s2=s1;
  27. reverse(s2.begin(),s2.end());
  28. cout<<test(s1,s2)<<endl;
  29. }
  30. }
Add Comment
Please, Sign In to add comment