poj3280题解

Cheapest Palindrome
Time Limit: 2000MS  Memory Limit: 65536K
Total Submissions: 5627  Accepted: 2734

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 the begining of the string, the cost would be 350 + 200 + 350 = 900, which is the minimum.
Source

USACO 2007 Open Gold
题目链接:http://poj.org/problem?id=3280
继续动态规划

题目解析:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <algorithm>
 4 #include <iostream>
 5 using namespace std;
 6 class DP {
 7         public:
 8                 void init();
 9                 void cal();
10         private:
11                 enum {
12                         MAXN = 26,
13                         MAXM = 2000};
14                 /*static const int MAXN = 26;
15                 static const int MAXM = 2000;*/
16                 static int dp_[MAXM + 2][MAXM + 2];
17                 //动态规划的数组,横下标表示从头到此的字符串,
18                 //纵下标表示从此到尾部的字符串。
19                 //对于一个不对称的字符串,分别处理左右两边的子串使之平衡
20                 int n_, m_, max_m_;
21                 int cost_[MAXN];
22                 char str_[MAXM + 2];
23                 int minco_[MAXM + 2];
24 };
25 int DP::dp_[MAXM + 2][MAXM + 2] = {0};
26 void DP::init() {
27         scanf("%d %d %s", &n_, &m_, str_ + 1);
28         char c[2];
29         max_m_ = m_ + 1;
30         for(int i = 1; i < max_m_; i++)
31                 str_[i] -= ‘a‘;
32         //对于字符串中每一个字符,抽象解释为0-25
33         int tmp1, tmp2;
34         for(int i = 0; i < n_; i++) {
35                 scanf("%s %d %d", c, &tmp1, &tmp2);
36                 cost_[c[0] - ‘a‘] = min(tmp1, tmp2);
37                 //对于单个对称字符的处理,加一个或减一个
38                 //该字符构成对称,结果一样,取消耗少的处理办法
39         }
40 }
41 void DP::cal() {
42         minco_[0] = 0;
43         for(int i = 1; i < max_m_; i++) {
44                 minco_[i] = minco_[i - 1] +
45                         cost_[str_[i]];
46         }//对于一段连续的字符子串,将它们逆向对称处理的最小消耗
47         for(int i = 0; i < max_m_; i++) {
48                 dp_[0][i + 1] = minco_[m_] - minco_[i];
49                 //对于左边的字符子串为0时,单独对称处理右子串的最小消耗
50                 dp_[i][max_m_] = minco_[i];
51                 //对于右边的字符子串为0时,道理一样
52         }
53         //已知边界条件
54         for(int i = 1; i < max_m_; i++) {
55                 for(int j = m_; j > i; j--) {
56                         int opi = dp_[i - 1][j] + cost_[str_[i]];
57                         int opj = dp_[i][j + 1] + cost_[str_[j]];
58                         int op = min(opi, opj);
59                         if(str_[i] == str_[j])
60                                 op = min(op, dp_[i - 1][j + 1]);
61                         dp_[i][j] = op;
62                 }
63         }
64         //每加入一个字符进行处理,可以处理左子串,右子串,
65         //若左右两端新加字符相同,则可以直接不用处理,
66         //三种状态取小者
67         int res = 0x7fffffff;
68         for(int i = 0; i < m_; i++) {
69                 res = min(res, dp_[i][i + 1]);
70                 res = min(res, dp_[i][i + 2]);
71         }
72         res = min(res, dp_[m_][max_m_]);
73         printf("%d\n", res);
74         //对于最终结果,必然要覆盖原来整个字符串,或者中间隔一个中心字符,
75         //对于右子串为空的情况,没有中心字符的情况,单独处理
76 }
77 int main(void)
78 {
79         DP dp;
80         dp.init();
81         dp.cal();
82         return 0;
83 }

该题必须注意到从非回文子串到回文子串的处理,对于同一个字母的对称处理,加减操作具有相同效果。
之后相对于已知的边界条件,推出动态规划的计算表达式就比较方便了

poj3280题解

时间: 2024-08-13 19:46:33

poj3280题解的相关文章

洛谷 P1079 Vigen&#232;re 密码 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=1079 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南北战争中为 南军所广泛使用. 在密码学中,我们称需要加密的信息为明文,用 M 表示:称加密后的信息为密文,用 C 表示:而密钥是一种

8.8联考题解

今天的T1让我怀疑我是不是在做奥赛题--这考的是什么知识点啊这个,会不会用绝对值函数? Evensgn 的债务 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Evensgn 有一群好朋友,他们经常互相借钱.假如说有三个好朋友A,B,C.A 欠 B 20 元,B 欠 C 20 元,总债务规模为 20+20=40 元.Evensgn 是个追求简约的人,他觉得这样的债务太繁杂了.他认为,上面的债务可以完全等价为 A 欠C20 元,B 既不欠别人,别人也不欠他.这样总债务规模就压缩到了 

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

2017ZZUACM省赛选拔试题部分题解----谨以纪念我这卡线滚粗的美好经历

写在前面: 其实心里有些小小的不爽又有点小小的舒畅,为啥捏?不爽当然是因为没被选拔上啦,舒畅捏则是因为没被选拔上反而让自己警醒,学长也提点很多很多."沉下去,然后一战成名"学长如是对我说,我很开心.其实这完全算不算是题解,只是我个人的一些小想法而已.而且到现在还有一题不会...让自己长点记性吧. 题目 A :聪明的田鼠 Time Limit: 1 Sec Memory Limit: 128 MB Description 田鼠MIUMIU来到了一片农田,农田可以看成是一个M*N个方格的矩

LeetCode-001题解

此题目摘自LeetCode001 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

leetcode题解: Next Permutation

最近还一直在刷leetcode,当然,更多时候只是将题解写在自己的电脑上,没有分享出来.偶尔想起来的时候,就写出来. public class Solution { public void nextPermutation(int[] nums) { if(nums==null||nums.length<=1) return; nextPermutationHelp( nums,0,nums.length-1); } public void nextPermutationHelp(int []nu

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

HDU 1045 Fire Net 二分图Bipartite题解

本题可以使用DFS直接爆搜出答案,不过这样类型的题目其实是个二分图的题解. 这个二分图,难不在Hungary算法,而是难在于建图.需要挺高的抽象思维的. 建图: 1 把同一行不被X分开的格子标同一个号码,被X分开的标下一个号码,这样做是为了缩点,不需要把所有的格子都分开标号,而且可以更方便建个更加小的图. 2 同理把同一列的格子标号 3 然后判断相同一个格子的行标号和列标号是有路径的,其他不在同一个格子的都是没有路径的. 4 这样就等于以行标号和列标号作为左右顶点,构建成一个二分图了 然后使用H