Software Industry Revolution

Source: Internet
Author: User

making revolutions in the software industry is not an easy task. that's why this problem is about something else. stanescu has just successfully Ted a new super-cool way to develop software. it is similar to writing program code, but instead
of writing it, you ask some else to do it. in such way, one cocould create great software, without even knowing what a Turing machine is. as you can see, this is not just software industry revolution. really, stanescu does not care about the software industry
at all. he just wants to make money.
in order to protect the money he is going to make, he needs to pick a special password for his bank account, satisfying the following requirements:

the password shocould not be too complex, so that stanescu can remember it. the complexity of a password is the sum of the complexity of its characters and the complexity of a character is its position in the alphabet (for 'A' it is 1, for 'B'-2, and so on ).
for example, the complexity of the string "Ala" is 1 + 12 + 1 = 14;
it shoshould match a given pattern string (composed of lowercase L Atin letters ,'? 'And' * ', no longer than 1000 characters ).'? 'Is matched by one arbitrary lowercase Latin letter, and' * '-by zero or more arbitrary lowercase Latin letters;
it shoshould be a sub-string of given super-password string (composed of lowercase Latin letters, no longer than 10000 ).

You have to write a program that computes the complexity of simplest possible password.
Input
Several test cases are given at the input. Each of them consists of a single line containing the pattern and the super-password strings separated by a white space.
Output
For each test case, your program shocould print a single line with one integer-the complexity of the simplest possible password. If no password satisfies the given requirements, the program shocould print-1.
Sample Input
A? A alabala
A * C? A axcbaabcbax
Sample output
4
9
Hint
Explanation:
Test Case #1: ABA is the simplest Password

Test Case #2: abcba is simpler than axcba

Question Analysis:

First of all, this question has been difficult for me for a long time, mainly because the matching string needs
Move one character behind all the letters and leave a zero digit blank.
If yes *, DP [0] [0] is assigned to zero. In this special case, the boundary condition is not very good.
We have been calling wa many times for a long time.
I will not say much about the question. I use DP, and many of you and I
I can't think of it at the beginning, but it does.
X [I] = '? 'Or X [I] = Y [J] DP [I] [J] = DP [I-1] [J-1] + Y [J]-'A' + 1
X [I] = '*' DP [I] [J] = DP [I-1] [k] + sum [J]-sum [k] deformation of equations,
DP [I] [J] = (DP [I-1] [k]-sum [k]) + sum [J];
Note: Many people think of monotonous queues. I have tried it and it times out,
Actually, this is not required. '*' can represent any long string. Therefore,
You only need to use now to keep a minimum value. This is the O (1) with full play)
Level

Coding:

# Include <stdio. h> # include <string. h> # define INF 100000000 # define min (a, B) (a <B? A: B) int DP [1005] [10005]; int main () {char X [1005], Y [10005]; int sum [10005]; int I, j, k; bool P; while (~ Scanf ("% S % s", x, y) {int lx, Ly; Lx = strlen (x); Ly = strlen (y); for (I = ly; i> = 1; I --) Y [I] = Y [I-1]; // move the sum [1] = Y [1]-'A' + 1; for (I = 1; I <= ly; I ++) {sum [I] = sum [I-1] + Y [I]-'A' + 1; // obtain the sum of the letters from the initial digit to the current digit} for (I = 0; I <lx; I ++) for (j = 0; j <= ly; j ++) DP [I] [J] = inf; for (I = 0; I <= ly; I ++) {if (I> 0 & (X [0] = '? '| X [0] = Y [I]) DP [0] [I] = Y [I]-'A' + 1; else if (X [0] = '*') {DP [0] [I] = 0; // initialize the first part of the original string} for (I = 1; I <lx; I ++) {P = false; If (X [I]> = 'A' & X [I] <= 'Z ') | x [I] = '? ') {For (j = 1; j <= ly; j ++) {If (DP [I-1] [J-1] = inf) continue; if (X [I] = Y [J] | x [I] = '? ') {DP [I] [J] = DP [I-1] [J-1] + Y [J]-'A' + 1; P = true ;}}} else {int now = inf; If (DP [I-1] [0] = 0) {DP [I] [0] = DP [I-1] [0]; // this step is easy to forget. Now = 0;} p = true; For (j = 1; j <= ly; j ++) {now = min (now, DP [I-1] [J]-sum [J]); // continuously update the minimum value if (now + sum [J] <DP [I] [J]) {DP [I] [J] = now + sum [J] ;}} if (! P) break;} int ans = inf; For (j = 0; j <= ly; j ++) {ans = min (ANS, DP [lx-1] [J]);} If (ANS = inf) ans =-1; printf ("% d \ n", ANS );}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.