/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
Solution s = new Solution();
Scanner sc
= new Scanner
(System.
in); //String a = sc.next();
//String b = sc.next();
//int ans = s.find(a , b); // your code goes here
sc.close();
}
}
class Solution{
//NUMBER OF subsequences in a that rae strinctLY greater thaN teh suBsequences in b
int n = A.length();
int m = B.length();
char[] a = A.toCharArray();
char[] b = B.toCharArray();
//comment on the dp state - >
/*
dp[i][j] = tells the number of subsequences from i-n-1 ..... that are strictly greater than j-------m-1
*/
int[][] dp = new int[n][m];
//filling the bottom row first
if(b[m-1] > a[n-1]){
dp[n-1][m-1] = 1;
}
for(int i = m-2 ; i>= 0 ; i--){
char froma = a[n-1];
char fromb = b[i];
if(froma > fromb){
//then teh answer ahead + thischaracter*allsubsequences ahread possible;
dp
[n
-1][i
] = dp
[n
-1][i
+1] + (int)(1*Math.
pow(2 , m
-i
-1) - 1); // that is the second figure is from fixing this character in B and all other subsequnces combined with this char }else if(froma < fromb){
dp[n-1][i] = dp[n-1][i+1];
}else{
//when both the current character are equal;
dp[n-1][i] = dp[n-1][i+1]; //because current As substring is just tshe last character .. if that is equal to the current character of B
//then it menas that we will consider the case of the subseq formed by the string ahead in B .. and ...
//no ostring made up of this character in B will be lesser than A as lexicograhically it will be longer
}
}
//now filling the last column ... means that we will be comparing the last character of B with all sbstring subsequences from last to first in A
for(int i = n-2 ; i>=0 ; i--){
char fromb = b[m-1];
char froma = a[i];
if(froma > fromb){ //basically all subsequesces in the string ahead except the null one
dp
[i
][m
-1] = dp
[i
+1][m
-1] + (int)(1*Math.
pow(2 , n
-i
-1) - 1);
}else if(froma < fromb){
dp[i][m-1] = dp[i+1][m-1];
}else{
//example A => abcd
//B => bcbb
//last character of B = b
//let current substring of A = bcd
//as b == b it means we will have to take all the cases of the prev substring that is cd + we will take the case where 'b, from A is compusalry
//and we want to take all the combinations of the ssubsequnces fromes from the substring ahead ... as As subsequence length after
//compulsarily taking 'b, from bcd we are left with cd .. and we will take all non null subsequences
dp
[i
][m
-1] = dp
[i
+1][m
-1] + (int)(1*Math.
pow(2 , n
-i
) -1);
}
}
//now for all the remianing
for(int i = n-2 ; i>= 0 ; i--){
for(int j = m-2 ; j>=0 ; j--){
char froma = a[i];
char fromb = b[j];
if(froma > fromb){ //fixing the current character at i in j
dp
[i
][j
] = dp
[i
+1][j
] + (int)(1*Math.
pow(2 , n
-1 - i
)); }else if(froma < fromb){
dp[i][j] = dp[i+1][j] ;
}else{
dp[i][j] = dp[i+1][j] + dp[i+1][j+1];
}
}
}
return dp[0][0];
}
}