• [BZOJ1710][Usaco2007 Open]Cheappal 廉价回文


    1710: [Usaco2007 Open]Cheappal 廉价回文

    Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 645  Solved: 361 [Submit][Status][Discuss]

    Description

    为了跟踪所有的牛,农夫JOHN在农场上装了一套自动系统. 他给了每一个头牛一个电子牌号 当牛走过这个系统时,牛的名字将被自动读入. 每一头牛的电子名字是一个长度为M (1 <= M <= 2,000) 由N (1 <= N <= 26) 个不同字母构成的字符串.很快,淘气的牛找到了系统的漏洞:它们可以倒着走过读 码器. 一头名字为"abcba"不会导致任何问题,但是名为"abcb"的牛会变成两头牛("abcb" 和 "bcba").农 夫JOHN想改变牛的名字,使得牛的名字正读和反读都一样.例如,"abcb"可以由在尾部添加"a".别的方法包 括在头上添加"bcb",得到"bcbabcb"或去掉"a",得到"bcb".JOHN可以在任意位置添加或删除字母.因为名字 是电子的,添加和删除字母都会有一定费用.添加和删除每一个字母都有一定的费用(0 <= 费用 <= 10,000). 对与一个牛的名字和所有添加或删除字母的费用,找出修改名字的最小的费用.空字符串也是一个合法的名字.

    Input

    * 第一行: 两个用空格分开的数, N 和 M.

    * 第二行: M个自符,初始的牛的名字.

    * 第3...N+2行: 每行含有一个字母和两个整数,分别是添加和删除这个字母的费用.

    Output

    一个整数, 改变现有名字的最小费用.

    Sample Input


    3 4
    abcb
    a 1000 1100
    b 350 700
    c 200 800
    输入解释:
    名字是 "abcb", 操作费用如下:

        添加   删除
    a  1000   1100
    b   350    700
    c   200    800

    Sample Output


    900
    输出解释:
    在尾部添加"a"得到"abcba"的费用为1000. 删除头上的"a",得到"bcb"的费用为1100.在头上添加"bcb"可以得到最小费用,350+200+350=900.
    可以发现加一个字符和删除效果是一样的,所以先把每个字符的代价取最小值
    然后设$f[i][j]$表示第$i$个字符到第$j$个字符中间弄成回文串的最小代价
    如果$str[i]=str[j]$则$f[i][j]=f[i+1][j-1]$
    否则$f[i][j] = min(f[i+1][j]+cost[str[i]],f[i][j-1]+cost[str[j]])$
    按照长度划分阶段即可递推
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    char buf[10000000], *ptr = buf - 1;
    inline int readint(){
        int f = 1, n = 0;
        char ch = *++ptr;
        while(ch < '0' || ch > '9'){
            if(ch == '-') f = -1;
            ch = *++ptr;
        }
        while(ch <= '9' && ch >= '0'){
            n = (n << 1) + (n << 3) + ch - '0';
            ch = *++ptr;
        }
        return f * n;
    }
    const int maxm = 2000 + 10;
    int N, M;
    char str[maxm];
    int cost[maxm];
    int f[maxm][maxm];
    int main(){
        fread(buf, sizeof(char), sizeof(buf), stdin);
        N = readint();
        M = readint();
        char t;
        for(int i = 1; i <= M; i++){
            t = *++ptr;
            while(t < 'a' || t > 'z') t = *++ptr;
            str[i] = t; 
        }
        for(int i = 1; i <= N; i++){
            t = *++ptr;
            while(t < 'a' || t > 'z') t = *++ptr;
            cost[t - 'a' + 1] = min(readint(), readint());
        }
        for(int i = 1; i <= M; i++) f[i][i] = 0;
        for(int len = 2; len <= M; len++)
            for(int j, i = 1; i <= M - len + 1; i++){
                j = i + len - 1;
                if(str[i] == str[j]) f[i][j] = f[i + 1][j - 1];
                else f[i][j] = min(f[i + 1][j] + cost[str[i] - 'a' + 1], f[i][j - 1] + cost[str[j] - 'a' + 1]);
            }
        printf("%d
    ", f[1][M]);
        return 0;    
    }
  • 相关阅读:
    [LC] 252. Meeting Rooms
    [LC] 243. Shortest Word Distance
    [LC] 215. Kth Largest Element in an Array
    [LC] 8. String to Integer (atoi)
    [LC] 367. Valid Perfect Square
    [LC] 66. Plus One
    [LC] 7. Reverse Integer
    [GeeksForGeeks] Print all nodes that don't have sibling in a binary tree.
    [Coding Made Simple] Maximum Sum Subsequence Non-adjacent
    [Coding Made Simple] Longest Bitonic Subsequence
  • 原文地址:https://www.cnblogs.com/ruoruoruo/p/7593600.html
Copyright © 2020-2023  润新知