fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc = new Scanner(System.in);
  14. String s = sc.next();
  15.  
  16. int n = s.length();
  17.  
  18. int[][] dp = new int[n][n];
  19.  
  20. int lps = 1;
  21. for(int i = 0; i < n; i++){
  22. dp[i][i] = 1;
  23. }
  24.  
  25. for(int i = 0; i < n-1; i++){
  26. if(s.charAt(i) == s.charAt(i+1)){
  27. dp[i][i+1] = 2;
  28. //lps = Math.max(lps, dp[i][i+1]);
  29. }else{
  30. dp[i][i+1] = 1;
  31. //lps = Math.max(lps, dp[i][i+1]);
  32. }
  33. }
  34.  
  35. for(int len = 3; len <= n; len++){
  36. for(int i = 0; i <= n - len; i++){
  37. int j = i + len - 1;
  38. if(s.charAt(i) != s.charAt(j)){
  39. dp[i][j] = Math.max(dp[i][j-1], dp[i+1][j]);
  40. }else{
  41. dp[i][j] = Math.max(2 + dp[i+1][j-1], Math.max(dp[i+1][j],dp[i+1][j-1]));
  42. }
  43. }
  44. }
  45.  
  46. System.out.print(dp[0][n-1]);
  47. }
  48. }
Success #stdin #stdout 0.12s 56596KB
stdin
aaabbbbb
stdout
5