Poj3898 Software Industry Revolution

Source: Internet
Author: User

Software Industry RevolutionTime Limit: 1000 MS Memory Limit: 65536 K Total Submissions: 263 Accepted: 86 Description 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 pass Word shoshould 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 shocould match a given pattern string (composed of lowercase Latin 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 singl E 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 alabalaa * c? A axcbaabcbaxSample Output 49 Hint Explanation: Test case #1: aba is the simplest password Test case #2: abcba is simpler than axcba, however, it is generally difficult for us to write the state transition equation. We have to look at the problem report for more than half a day to know what it means. I almost copied the problem report, even though I did it myself, ah .... Just be the interpreter.

/* Dp [I] [j] indicates the minimum value when the I of the mode string matches the j of the original string. For? Dp [I] [j] = dp [I-1] [J-1] + cost [j, j] for letters, mat [I] = pat [j] is required for transfer. Dp [I] [j] = dp [I-1] [J-1] + cost [j, j] sum [I] indicates the sum of the I cost before the original string for * dp [I] [j] = min {dp [I-1] [k] + cost [k + 1, j]} for k> = 0 & k <= j for this formula, it seems that K is required to be enumerated, which obviously does not work. We can convert dp [I-1] [k] + cost [k + 1, j] = dp [I-1] [k] + sum [j]-sum [k] = (dp [I-1] [k]-sum [k]) + sum [j] (dp [I-1] [k]-sum [k]) This is a formula that only relates to k. We can save a (dp [I-1] [k]-sum [k]) minimum value. In this way, o (1) can get dp [I] [j. */# Include <stdio. h> # include <iostream> # include <string. h> using namespace std; char str [1050], pass [10050]; int sum [10050], record [27], dp [1005] [10050]; int main () {int I, j, cur, n, m; bool flag; while (scanf ("% s", str + 1, pass + 1 )! = EOF) {n = strlen (str + 1); m = strlen (pass + 1); sum [0] = 0; for (I = 0; I <26; I ++) record [I] = 0; for (I = 1; I <= m; I ++) {record [pass [I]-'a'] ++;} for (I = 1; I <= n; I ++) {if (str [I]> = 'A') // I didn't expect the problem to happen here, when the array is out of bounds, the oj does not prompt RE, but prompts WA record [str [I]-'a'] --;} for (I = 0; I <26; I ++) {if (record [I] <0) break;} if (I <26) // This is the initial optimization, remove {printf ("-1 \ n"); continue ;}for (I = 1; I <= m; I ++) {sum [I] = pass [I]-'A' + 1 + sum [I-1];} for (I = 0; I <= n; I ++) for (j = 0; J <= m; j ++) dp [I] [j] = 10000000; for (I = 0; I <= m; I ++) // special operation for str [1]. Give dp [1] [j] an initial value {if (I> 0 & (str [1] = '? '| Str [1] = pass [I]) // is the first? Or equal to pass [I] That dp [1] [I] Of course is str [1] corresponding value is also sum [I]-sum [I-1] {dp [1] [i] = sum [I]-sum [I-1];} else if (str [1] = '*') // * is a bit special. Its value is not fixed, but this requires the smallest value. if the first str is *, then we can get no value, that is, 0 {dp [1] [I] = 0 ;}} for (I = 2; I <= n; I ++) {flag = 0; if (str [I] = '*') {cur = 10000000; if (dp [I-1] [0] = 0) {flag = 1; dp [I] [0] = 0; cur = 0; // record the minimum value} for (j = 1; j <= m; j ++) {if (dp [I-1] [j]-sum [j] <cur) cur = dp [I-1] [j]-sum [j]; if (sum [j] + cur <dp [I] [j]) {dp [I] [j] = s Um [j] + cur; flag = 1 ;}} else {for (j = 1; j <= m; j ++) {if (dp [I-1] [J-1] = 10000000) continue; // simplifies the operation if (str [I] = '? '| Str [I] = pass [j]) {if (dp [I] [j]> dp [I-1] [J-1] + pass [j]-'A' + 1) {dp [I] [j] = dp [I-1] [J-1] + pass [j]-'A' + 1; flag = 1 ;}}} if (! Flag) // if the flag is 0, it indicates that there are no operations above, no matching conditions are met, and break cannot be found;} if (I <= n) // not all programs are executed. str does not find {printf ("-1 \ n"); continue;} cur = 100000000; for (I = 0; I <= m; I ++) // It should start from scratch. if str is *, the result is 0 {if (cur> dp [n] [I]). // all the letters in str are found. cur = dp [n] [I];} if (cur = 100000000) printf ("-1 \ n "); else printf ("% d \ n", cur);} return 0 ;}

 


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.