Software Industry Revolution

Time limit1sMemory limit128 MB

Summary
Given a wildcard pattern (with ? and *) and a text, find the minimum-complexity substring of the text that matches the whole pattern, or report impossible.
Level

Hard8 of 10

Topics
String matching, Dynamic programming, String
Solved
No attempts yet

Problem

Making revolutions in the software industry is not easy, so this problem is about something else. Stanescu has just invented a new, super-cool way to develop software: it is similar to writing program code, but instead of writing it yourself, you ask someone else to do it. This way anyone could create great software without even knowing what a Turing machine is. Stanescu does not really care about the software industry at all — he just wants to make money.

To protect the money he is about to make, he must choose a special password for his bank account that satisfies all of the following requirements:

  • The password must not be too complex, so that Stanescu can remember it. The complexity of a password is the sum of the complexities of its characters, and the complexity of a character is its position in the alphabet (a is 1, b is 2, and so on). For example, the complexity of the string ala is 1+12+1=141 + 12 + 1 = 14.
  • It must match a given pattern string (composed of lowercase Latin letters, ? and *, and no longer than 1000 characters). A ? is matched by exactly one arbitrary lowercase Latin letter, and a * is matched by zero or more arbitrary lowercase Latin letters. The whole password must match the whole pattern.
  • It must be a substring of a given super-password string (composed of lowercase Latin letters and no longer than 10000 characters).

Write a program that computes the complexity of the simplest possible password (the one with the smallest complexity).

Input

Several test cases are given in the input. Each of them consists of a single line containing the pattern string and the super-password string, separated by white space.

Output

For each test case, print a single line with one integer: the complexity of the simplest possible password. If no password satisfies the given requirements, print -1.

Examples1

  1. Example 1

    Input
    a?a alabala
    a*c?a axcbaabcbax
    
    Expected output
    4
    9