动态规划升级篇,一些变形
来源:互联网 发布:淘宝有哪些好玩的东西 编辑:程序博客网 时间:2024/06/09 14:00
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 ofN (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 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
Sample Input
3 4abcba 1000 1100b 350 700c 200 800
Sample Output
900
Hint
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int inf=1<<30;int ans[2005][2005];int map[30][2];int main(){int n,m;char c;while(cin>>n>>m){char s[m];for (int i=0;i<2005;i++){for (int j=0;j<2005;j++)if(i>=j) ans[i][j]=0;else ans[i][j]=inf;}scanf("%s",s);//cout<<s<<endl;for (int i=0;i<n;i++){cin>>c;cin>>map[c-'a'][0]>>map[c-'a'][1];}for (int i=1;i<m;i++){for (int j=0;j+i<m;j++){if (s[j]!=s[j+i]){ans[j][j+i]=min(ans[j][j+i],ans[j+1][j+i]+min(map[s[j]-'a'][0],map[s[j]-'a'][1]));ans[j][j+i]=min(ans[j][j+i],ans[j][j+i-1]+min(map[s[j+i]-'a'][0],map[s[j+i]-'a'][1]));}else ans[j][j+i]=ans[j+1][j+i-1];}}cout<<ans[0][m-1]<<endl;}}
- 动态规划升级篇,一些变形
- <接上一篇> 动态规划一些变形 poj 1742 poj3046
- 【动态规划 变形】fight club
- 动态规划——背包问题变形
- 动态规划(完全背包的变形)
- 动态规划一些实现
- 动态规划的一些思考
- 动态规划的一些整理
- 动态规划的一些小结
- 动态规划的一些题
- 动态规划的一些程序
- 动态规划的一些笔记
- 动态规划的一些总结
- 动态规划的一些资料
- ZJU 2604 Little Brackets - 动态规划 Catalan数变形
- 动态规划——背包问题变形 收藏
- [usaco]3.4.4 变形的动态规划问题Electric Fence
- 【动态规划 变形】天堂 heaven.pas/c/cpp
- 调整数组顺序使奇数位于偶数前面Java
- Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释
- 和OpenCV1的互通性
- 最好用的发短信(验证码、语音短信)接口(PHP)
- [LeetCode]13. Roman to Integer
- 动态规划升级篇,一些变形
- 有关求最大不含重复字符子字符串算法的C#实现
- HashMap源码分析
- Bigdata 3.0:Presto+Elasticsearch研发进展
- 华为oj 计算字符串的相似度
- UIView的alpha、hidden和opaque属性之间的关系和区别
- Poj 1603 Risk 【最短路】
- StringBuilder详解
- Android Property动画(属性动画)