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 in=new Scanner(System.in);
  14.  
  15. String a = in.nextLine();
  16. String b = in.nextLine();
  17.  
  18. int [][] dp=new int [a.length()+1][b.length()+1];
  19.  
  20. for(int i=a.length()-1;i>=0;i--){
  21. for(int j = b.length() - 1; j >= 0; j--){
  22. if(a.charAt(i)>b.charAt(j)){
  23. int len = a.length()-i-1;
  24. dp[i][j]=dp[i+1][j]+ (int)(Math.pow(2,len));
  25. }else if(a.charAt(i)<b.charAt(j)){
  26. dp[i][j]=dp[i+1][j];
  27. }else if(a.charAt(i)==b.charAt(j)){
  28. dp[i][j]=dp[i+1][j] + dp[i+1][j+1];
  29. }
  30. }
  31. }
  32. System.out.println(dp[0][0]);
  33.  
  34. in.close();
  35. }
  36. }
Success #stdin #stdout 0.12s 54640KB
stdin
baduy
ac
stdout
30