• POJ 3280 Cheapest Palindrome


    Cheapest Palindrome

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 6   Accepted Submission(s) : 3
    Problem Description

    Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag's contents are currently a single string with length M (1 ≤ M ≤ 2,000) characters drawn from an alphabet of N (1 ≤ N ≤ 26) different symbols (namely, the lower-case roman alphabet).

    Cows, being the mischievous creatures they are, sometimes try to spoof the system by walking backwards. While a cow whose ID is "abcba" would read the same no matter which direction the she walks, a cow with the ID "abcb" can potentially register as two different IDs ("abcb" and "bcba").

    FJ would like to change the cows's ID tags so they read the same no matter which direction the cow walks by. For example, "abcb" can be changed by adding "a" at the end to form "abcba" so that the ID is palindromic (reads the same forwards and backwards). Some other ways to change the ID to be palindromic are include adding the three letters "bcb" to the begining to yield the ID "bcbabcb" or removing the letter "a" to yield the ID "bcb". One can add or remove characters at any location in the string yielding a string longer or shorter than the original string.

    Unfortunately as the ID tags are electronic, each character insertion or deletion has a cost (0 ≤ cost ≤ 10,000) which varies depending on exactly which character value to be added or deleted. Given the content of a cow's ID tag and the cost of inserting or deleting each of the alphabet's characters, find the minimum cost to change the ID tag so it satisfies FJ's requirements. An empty ID tag is considered to satisfy the requirements of reading the same forward and backward. Only letters with associated costs can be added to a string.

     
    Input
    Line 1: Two space-separated integers: <i>N</i> and <i>M</i> <br>Line 2: This line contains exactly <i>M</i> characters which constitute the initial ID string <br>Lines 3..<i>N</i>+2: Each line contains three space-separated entities: a character of the input alphabet and two integers which are respectively the cost of adding and deleting that character.
     
    Output
    Line 1: A single line with a single integer that is the minimum cost to change the given name tag.
     
    Sample Input
    3 4 abcb a 1000 1100 b 350 700 c 200 800
     
    Sample Output
    900
     
     
    区间DP
    非原创
    =================================================================================================
            定义
                dp[i][j]:=从第i个字符到第j个字符构造回文字符串最少需要多少费用。

            于是有,
                当s[i] == s[j]时,dp[i][j] = dp[i+1][j-1]
            
                否则,
                    因为一个字符的删除和插入操作时等价的,要在左边插入,就可以在右边删除,
                反之,类似。
                    
                    定义,w[ch - 'a']:= 字符操作ch所用的最小代价,即min(cost(插入), cost(删除))
                
                因此,
                    dp[i][j] = min(dp[i-1][j] + w[s[i] - 'a'], dp[i][j - 1] + w[s[j] - 'a'])

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 #include<stdio.h>
     6 #include<algorithm>
     7 #include<stack>
     8 using namespace std;
     9 int ad[26], dp[2005][2005];
    10 char str[2005];
    11 int main()
    12 {
    13     int n, m;
    14     while (cin >> n >> m)
    15     {
    16         cin >> str;
    17         memset(dp, 0, sizeof(dp));
    18         int add, del;
    19         char x;
    20         int i,j;
    21         for (i = 0; i <n; i++)
    22         {
    23             cin >> x >> add >> del;
    24             ad[x - 'a'] = min(add, del);
    25         }
    26         for (j = 1; j<m; j++)
    27         {
    28             for (i = j - 1; i >= 0; i--)//求出每个区间的价值,保存最小的价值
    29             {
    30                 dp[i][j] = min(dp[i + 1][j] + ad[str[i] - 'a'], dp[i][j - 1] + ad[str[j] - 'a']);
    31                 if (str[i] == str[j])//已经是回文串
    32                     dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]);
    33             }
    34         }
    35         cout << dp[0][m - 1] << endl;
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    2015第二周日
    2015第二周六
    2015第二周五
    反思java web的发展
    servlet/filter/listener/interceptor区别与联系
    WSSecurity简述
    2015第2周一数据传输安全
    2015第一周日
    2015第1周六2015技术努力方向
    插入排序
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8434955.html
Copyright © 2020-2023  润新知