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