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.  
  15. String s = sc.nextLine();
  16. Integer n = sc.nextInt();
  17.  
  18. int[][] queries = new int[n][2];
  19.  
  20. for(int i = 0; i < n; i++){
  21. queries[i][0] = sc.nextInt() - 1;
  22. queries[i][1] = sc.nextInt() - 1;
  23. }
  24.  
  25. Integer m = s.length();
  26.  
  27. int[][] dp = new int[m + 1][m + 1];
  28. int[][] ans = new int[m + 1][m + 1];
  29.  
  30. for(int i = 0; i < m; i++){
  31. dp[i][i] = 1;
  32. ans[i][i] = 1;
  33. }
  34.  
  35. for(int i = 0; i < m-1; i++){
  36. if(s.charAt(i) == s.charAt(i+1)){
  37. dp[i][i+1] = 1;
  38. }
  39. ans[i][i+1] = dp[i][i] + dp[i+1][i+1] + dp[i][i+1];
  40.  
  41. }
  42.  
  43. for(int len = 3; len <= m; len++){
  44. for(int i = 0; i <= m - len; i++){
  45. int j = i + len - 1;
  46.  
  47. if(s.charAt(i) == s.charAt(j) && dp[i+1][j-1] == 1){
  48. dp[i][j] = 1;
  49. }
  50.  
  51. ans[i][j] = ans[i][j-1] + ans[i+1][j] - ans[i+1][j-1] + dp[i][j];
  52. }
  53. }
  54.  
  55. for(int[] q : queries){
  56. int count = 0;
  57. for(int i = q[0]; i <= q[1]; i++){
  58. for(int j = i; j <= q[1]; j++){
  59. System.out.println(dp[i][j]);
  60. }
  61. }
  62.  
  63. }
  64. }
  65. }
Success #stdin #stdout 0.11s 56628KB
stdin
abacaba
6
1 7
2 6
3 5
1 3
4 4
5 7
stdout
1
0
1
0
0
0
1
1
0
0
0
1
0
1
0
1
0
0
1
0
0
0
1
0
1
1
0
1
1
0
0
0
1
1
0
1
0
1
0
0
1
0
1
1
0
1
1
0
1
1
0
1
1
0
1
1
1
0
1
1
0
1