Subsequences in Substrings

Time limit2sMemory limit512 MB

Summary
Count how many substrings of s contain t as a subsequence at least once.
Level

Medium7 of 10

Topics
Two pointers, Dynamic programming, String, Greedy
Solved
No attempts yet

Problem

You are given two strings ss and tt. Count the number of substrings of ss that contain tt as a subsequence at least once.

A substring and a subsequence both consist of characters from the original string, in order. In a substring, the characters must be contiguous in the original string. In a subsequence, they do not have to be contiguous. In the string abcde, ace is a subsequence but not a substring.

If ss is aa and tt is a, the answer is 3: [a]a, [aa], and a[a].

Input

Each test case consists of exactly two lines.

The first line contains string ss (1≤∣s∣≤1051 \le |s| \le 10^5, s∈[a−z]∗s \in [a-z]^*), with no other characters. The second line contains string tt (1≤∣t∣≤1001 \le |t| \le 100, ∣t∣≤∣s∣|t| \le |s|, t∈[a−z]∗t \in [a-z]^*), with no other characters.

Output

Output a single integer: the number of substrings of ss that contain tt as a subsequence at least once.

Examples3

  1. Example 1

    Input
    abcdefghijklmnopqrstuvwxyz
    a
    
    Expected output
    26
    
  2. Example 2

    Input
    abcdefghijklmnopqrstuvwxyz
    m
    
    Expected output
    182
    
  3. Example 3

    Input
    penpineappleapplepen
    ppap
    
    Expected output
    68