Nyoj 1023 or retrieval (DP, which forms a retrieval string at least cost)

Source: Internet
Author: User
1/* 2 meaning: it takes a certain amount of money to add or delete a string of characters (all in lowercase letters. 3. What is the minimum cost of converting a string into a return string? 4 5 train of thought: if a character string can be added with a character X to form a return string, then deleting this character x 6 from this string can also form a return string! 7. Therefore, we only need to delete the records and add the minimum cost of this character X! -> The minimum cost of converting to the number of characters added to form a text return string! 8 9 STR [I]! = STR [k] 10 DP [I] [J] = min (DP [I] [J-1] + cost [STR [k]-'a'], DP [I + 1] [J-1] + cost [STR [I]-'a']); 11 12 STR [I] = STR [k] 13 DP [I] [J] = DP [I + 1] [J-2]; 14 15 */16 # include <iostream> 17 # include <cstring> 18 # include <cstdio> 19 # include <algorithm> 20 # define n 2005 21 using namespace STD; 22 23 int DP [N] [N]; 24 25 int cost [30]; 26 27 char STR [N]; 28 29 int main () {30 int M, N; 31 While (scanf ("% d", & M, & N )! = EOF) {32 scanf ("% s", STR + 1); 33 memset (cost, 0, sizeof (cost); 34 while (M --) {35 char ch; 36 int A, B; 37 getchar (); 38 scanf ("% C % d", & Ch, & A, & B ); 39 cost [CH-'a'] = min (a, B); 40} 41 for (INT I = 1; I <= N; ++ I) 42 DP [I] [1] = 0; 43 for (Int J = 2; j <= N; ++ J) 44 for (INT I = 1; I + J-1 <= N; ++ I) {45 int K = I + J-1; 46 If (STR [I]! = STR [k]) 47 DP [I] [J] = min (DP [I] [J-1] + cost [STR [k]-'a'], DP [I + 1] [J-1] + cost [STR [I]-'a']); 48 else DP [I] [J] = DP [I + 1] [J-2]; 49} 50 51 printf ("% d \ n ", DP [1] [N]); 52} 53 return 0; 54}

 

Nyoj 1023 or retrieval (DP, which forms a retrieval string at least cost)

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.