zoukankan      html  css  js  c++  java
  • poj3280

    Cheapest Palindrome
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 10628   Accepted: 5093

    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

    Hint

    If we insert an "a" on the end to get "abcba", the cost would be 1000. If we delete the "a" on the beginning to get "bcb", the cost would be 1100. If we insert "bcb" at thebegining of the string, the cost would be 350 + 200 + 350 = 900, which is the minimum.
     
    解题思路:
    dp
    实际上根本不用考虑是添加还是删除,哪个便宜就那样。
    比如说“ab”若想使他变成回文串,可以加b,可以减b,可以加a,可以减a
    dp[i][j]表示从i到j成为回文串的最小花费
     
    //转

    题目大意是说一个字符串,每插入或者删除一个字符都需要一定的代价,问怎样可以使这个字符串变成一个回文串,且花费最小。

    首先明确的就是如果已经将区间[i,j]整理成为一个回文串(不管中间有多少个字符或者是以什么字符开头或者结尾),当DP到区间[i,j+1]时,我们可以在i-1的位置添加一个str[j+1]字符,或者将在j+1处的字符删除,得到一个新的回文串,而且我们这两步操作都没有借助或者影响区间[i,j]的情况。

    因此,那我们就可以将添加或者删除整合在一起,对字符str[j+1]的操作就按照添加和删除中花费最小的一个计算。写出状态转移方程:

              DP[j][i] = MIN(DP[j+1][i]+cost[str[j]-'a'], DP[j][i-1]+cost[str[i]-'a']);           if(str[i] == str[j])DP[j][i] = MIN(DP[j][i],DP[j+1][i-1]);

    这里的j是按照i-1到0枚举的,由于可能str[i] == str[j],因此也可以不操作,再将  不操作  与  添加或删除区间头或尾  比较。

    当然,其实最初的想法是扩展出来四个状态,就是首删除,首添加,尾删除,尾添加,由此扩展开来求一个最小值

    //

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdlib>
     6 #include<algorithm>
     7 
     8 using namespace std;
     9 
    10 int n,m,add,del;
    11 int cost[30],dp[2010][2010];
    12 char ch[2010],temp;
    13 
    14 int read()
    15 {
    16     char ch=getchar();
    17     int x=0,f=1;
    18     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    19     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    20     return x*f;
    21 }
    22 int main()
    23 {
    24     n=read();m=read();
    25     for(int i=0;i<m;i++)scanf("%c",&ch[i]);
    26     for(int i=1;i<=n;i++)
    27     {
    28         scanf("%s",&temp);
    29         add=read();
    30         del=read();
    31         cost[temp-'a']=min(add,del);
    32     }
    33     for(int i=1;i<=m-1;i++)
    34     {
    35         for(int j=i-1;j>=0;j--)
    36         {
    37             dp[j][i]=min(dp[j+1][i]+cost[ch[j]-'a'],dp[j][i-1]+cost[ch[i]-'a']);
    38             if(ch[j]==ch[i])dp[j][i]=min(dp[j][i],dp[j+1][i-1]);
    39         }
    40     }
    41     printf("%d",dp[0][m-1]);
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    千万数量级分页存储过程(效果演示)
    hibernate注解实体类(Dept.java)
    hibernate注解实体类(Dept.java)
    hibernate注解的测试
    hibernate注解的测试
    hibernate注解的测试
    Hibernate中使用Criteria查询及注解——( EmpCondition)
    Hibernate中使用Criteria查询及注解——( EmpCondition)
    Hibernate中使用Criteria查询及注解——( EmpCondition)
    Hibernate中使用Criteria查询及注解——(DeptTest.java)
  • 原文地址:https://www.cnblogs.com/taojy/p/7515599.html
Copyright © 2011-2022 走看看