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[][] dpCount = new int[n][n];
  19. int[][] dp = new int[n][n];
  20.  
  21. for(int i = 0; i < n; i++){
  22. dp[i][i] = 1;
  23. dpCount[i][i] = 1;
  24. }
  25.  
  26. for(int i = 0; i < n-1; i++){
  27. if(s.charAt(i) == s.charAt(i+1)){
  28. dp[i][i+1] = 1;
  29. dpCount[i][i+1] = 3;
  30. }else{
  31. dpCount[i][i+1] = 2;
  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.  
  39. dpCount[i][j] = dpCount[i][j-1] + dpCount[i+1][j] - dpCount[i+1][j-1];
  40.  
  41. if(s.charAt(i) == s.charAt(j) && dp[i+1][j-1] == 1){
  42. dp[i][j] = 1;
  43. dpCount[i][j] += 1;
  44. }
  45. }
  46. }
  47.  
  48. int stick = 0;
  49. int count = 0;
  50.  
  51. while(stick < n - 1){
  52. int b = 0;
  53. for(int ip = stick ; ip >= 0; ip--){
  54. if(dp[ip][stick] == 1){
  55. b++;
  56. }
  57. }
  58. int y = dpCount[stick+1][n-1];
  59. count += b * y;
  60. stick++;
  61. }
  62. System.out.print(count);
  63. }
  64. }
Success #stdin #stdout 0.17s 54476KB
stdin
abaabb
stdout
30