zoukankan      html  css  js  c++  java
  • poj3280 Cheapest Palindrome

    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: N and M 
    Line 2: This line contains exactly M characters which constitute the initial ID string 
    Lines 3..N+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

    给你一个m个字符的字符串以及每种字符增加或减少一个的价格,问最少多少价格能使其变为回文序列。

    思路:因为增加和减少都是相对的,所以可以得出删减每种字符的价格,然后用dp[i][j]表示i到j区间使这些字符变为回文串的最小价格。如果s[i]==s[j],dp[i][j]=min(dp[i][j],dp[i+1][j-1]);如果s[i]!=s[j],dp[i][j]=min(dp[i+1][j]+w[s[i]-'a'],dp[i][j-1]+w[s[j]-'a'].

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define maxn 2060
    #define inf 999999999
    int dp[2005][2006];
    char s[maxn],str[10];
    int w[30];
    int main()
    {
        int n,m,i,j,c,d,len;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            scanf("%s",s+1);
            for(i=1;i<=n;i++){
                scanf("%s%d%d",str,&c,&d);
                w[str[0]-'a']=min(c,d);
            }
            if(m==1){
                printf("0
    ");continue;
            }
    
            for(i=1;i<=m;i++){
                dp[i][i]=0;
            }
    
            for(i=1;i<=m-1;i++){
                if(s[i]==s[i+1])dp[i][i+1]=0;
                else dp[i][i+1]=min(w[s[i]-'a' ],w[s[i+1]-'a']);
                /*printf("--->%d
    ",dp[i][i+1]);*/
            }
            if(m==2){
                printf("%d
    ",dp[1][2]);continue;
            }
    
            for(len=3;len<=m;len++){
                for(i=1;i+len-1<=m;i++){
                    j=i+len-1;
                    dp[i][j]=inf;
                    if(s[i]==s[j]){
                        dp[i][j]=dp[i+1][j-1];
                    }
                    else{
                        dp[i][j]=min(dp[i+1][j]+w[s[i]-'a' ],dp[i][j-1]+w[s[j]-'a'  ]);
                    }
                }
    
            }
            printf("%d
    ",dp[1][m]);
        }
        return 0;
    }
    


  • 相关阅读:
    老毛桃PE修改方法(屏蔽更改主页,屏蔽加装的绿色浏览器)
    老毛桃winpe优盘启动系统个性修改全攻略
    SQL2000和SQL2005和SQL2008同时安装问题
    基于FFmpeg的音频编码(PCM数据编码成AAC android)
    springboot + mybatis +easyUI整合案例
    spring thymeleaf 自定义标签
    速度挑战
    兼顾pc和移动端的textarea字数监控的实现方法
    CSS3+JS 实现的便签应用
    JavaScript实现碰撞检测(分离轴定理)
  • 原文地址:https://www.cnblogs.com/herumw/p/9464676.html
Copyright © 2011-2022 走看看