zoukankan      html  css  js  c++  java
  • (中等) POJ 3280 Cheapest Palindrome,DP。

    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.

      题目就是让把一个字符串变成一个回文串,可以任意删加,求最小代价。

      典型的DP问题,对于这个题可以联想到那个经典的题目,就是把两个字符串变成完全相同的最小代价,这个的话枚举向左和向右的两个字符串,然后一次求出变成相同的最小代价就好了。

      dp[i][j]表示i向左的那个串和j向右的那个串变成完全相同的代价。

    代码如下:

    // ━━━━━━神兽出没━━━━━━
    //      ┏┓       ┏┓
    //     ┏┛┻━━━━━━━┛┻┓
    //     ┃           ┃
    //     ┃     ━     ┃
    //     ████━████   ┃
    //     ┃           ┃
    //     ┃    ┻      ┃
    //     ┃           ┃
    //     ┗━┓       ┏━┛
    //       ┃       ┃
    //       ┃       ┃
    //       ┃       ┗━━━┓
    //       ┃           ┣┓
    //       ┃           ┏┛
    //       ┗┓┓┏━━━━━┳┓┏┛
    //        ┃┫┫     ┃┫┫
    //        ┗┻┛     ┗┻┛
    //
    // ━━━━━━感觉萌萌哒━━━━━━
    
    // Author        : WhyWhy
    // Created Time  : 2015年07月19日 星期日 16时29分25秒
    // File Name     : 3280.cpp
    
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    const int MaxN=2010;
    const long long INF=1000000000000000LL;
    
    int N,M;
    char s[MaxN];
    long long dp[MaxN][MaxN];
    long long aC[MaxN],dC[MaxN];
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    
        int a,b;
        char ts[10];
        long long sum1,sum2;
    
        while(~scanf("%d %d",&N,&M))
        {
            scanf("%s",s);
            memset(aC,0,sizeof(aC));
            memset(dC,0,sizeof(dC));
    
            for(int i=1;i<=N;++i)
            {
                scanf("%s %d %d",ts,&a,&b);
                aC[ts[0]-'a']=a;
                dC[ts[0]-'a']=b;
            }
    
            sum1=sum2=0;
            dp[0][M]=0;
    
            for(int j=M-1;j>=0;--j)
            {
                sum1+=aC[s[j]-'a'];
                sum2+=dC[s[j]-'a'];
                dp[0][j]=min(sum1,sum2);
            }
    
            sum1=sum2=0;
            
            for(int i=1;i<=M;++i)
            {
                sum1+=aC[s[i-1]-'a'];
                sum2+=dC[s[i-1]-'a'];
                dp[i][M]=min(sum1,sum2);
            }
    
            for(int i=1;i<=M;++i)
                for(int j=M-1;j>=i;--j)
                    if(s[i-1]==s[j])
                        dp[i][j]=dp[i-1][j+1];
                    else
                        dp[i][j]=min(dp[i-1][j]+min(aC[s[i-1]-'a'],dC[s[i-1]-'a']),dp[i][j+1]+min(aC[s[j]-'a'],dC[s[j]-'a']));
    
            long long minn=INF;
    
            for(int i=0;i<=M;++i)
                minn=min(minn,dp[i][i]);
    
            for(int i=0;i<M;++i)
                minn=min(minn,dp[i][i+1]);
    
            printf("%lld
    ",minn);
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    SQL Data Services Abandons REST for TDS API and Knocks My Socks Off
    C# Lambda Expressions 简介
    Web.config 和 App.config 区别
    XPO 第三方控件学习(DevExpress Persistent Object )系列查询
    进销存管理系统 系列
    MOSS 学习系列(十万个为什么)--为什么我要定制审批流程的时候找不到审批的选项?
    XPO 第三方控件学习(DevExpress Persistent Object )系列 其他(session,事务,有效性检查...
    如何使用三态工作流(一)
    english interview example
    How to build a K2 SharePoint Workflow(2)
  • 原文地址:https://www.cnblogs.com/whywhy/p/4660552.html
Copyright © 2011-2022 走看看