OpenKattis
Kattis Set 10 - String Processing and BSTA+Other

Start

2021-03-22 05:00 AKDT

Kattis Set 10 - String Processing and BSTA+Other

End

2021-03-29 01:30 AKDT
The end is near!
Contest is over.
Not yet started.
Contest is starting in -312 days 6:06:32

164:30:00

0:00:00

Problem AOrderly Class

Ms. Thomas is managing her class of $n$ students.

She placed all her students in a line, and gave the $i$-th student from the left a card with the letter $a_ i$ written on it.

She would now like to rearrange the students so that the $i$-th student from the left has a card with the letter $b_ i$ written on it.

To do this, she will choose some consecutive group of students, and reverse their order. Students will hold on to their original cards during this process.

She’s now wondering, what is the number of valid ways to do this? (It may be impossible, in which case, the answer is zero).

With sequences $abba$ and $aabb$, Ms. Thomas can choose the group $a(bba)$. With sequences $caxcab$ and $cacxab$, Ms. Thomas can choose $ca(xc)ab$ or $c(axca)b$. With sequences $a$ and $z$, there are clearly no solutions.

Input

The input is two lines of lowercase letters, $A$ and $B$. The $i$-th character of $A$ and $B$ represent $a_ i$ and $b_ i$ respectively. It is guaranteed that $A$ and $B$ have the same positive length, and $A$ and $B$ are not identical. The common length is allowed to be as large as $100\, 000$.

Output

For each test case, output a single integer, the number of ways Ms. Thomas can reverse some consecutive group of $A$ to form the line specified by string $B$.

Sample Input 1 Sample Output 1
abba
aabb

1

Sample Input 2 Sample Output 2
caxcab
cacxab

2

Sample Input 3 Sample Output 3
a
z

0