poj3280Cheapest Palindrome
给定一个字符串S,字符串S的长度为M(M≤2000),字符串S所含有的字符的种类的数量为N(N≤26),然后给定这N种字符Add与Delete的代价,求将S变为回文串的最小代价和。
Input
第一行:两个由空格分隔的整数 N 和 M
第二行:这一行给出了恰好 M 个字符,表示初始状态下的ID字符串
接下来的 N 行:每一行给出了由空格分隔的三部分。首先是一个字符,保证出现在了输入的字符串中。接下来是两个整数,表示你增添这个字符的代价,然后是删除这个字符的代价
Output
你只需要输出一行,且只输出一个整数。表示你将给定字符串变成回文串所需的最小代价。
Sample Input
3 4
abcb
a 1000 1100
b 350 700
c 200 800
Sample Output
900
dp[i][j]表示使区间[i,j]变为回文串所要耗费的最少能量。
include
using namespace std;
int dp[2005][2005],w[130]; //这里之前写进main()函数,提交一直显示runtime error,听人说貌似数组开的大的话,一般都是开在全局变量。
int main()
{
int n,m,a,b;
char s[2100],ch;
while(cin>>n>>m)
{
// getchar();
cin>>s;
for(int i=0;i<n;i++)
{
cin>>ch>>a>>b;
w[ch-'a']=min(a,b);
}
for(int i=m-1;i>=0;i--)
{
for(int j=i+1;j<m;j++)
{
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']);
}
}
cout<<dp[0][m-1]<<endl;
}
return 0;
}
poj3280Cheapest Palindrome的更多相关文章
- poj3280Cheapest Palindrome(记忆化)
链接 真的1A了.. 一开始想复杂了 想着补全再删 没想好 后来想到递归 大的回文串是由小的推过来的 一直递归下去 对于当前的i,j可以选择保留或者删除 选个最小的 #include <iost ...
- POJ3280--Cheapest Palindrome(动态规划)
Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate ...
- 【动态规划】POJ3280- Cheapest Palindrome
[题目大意] 给出一个字符串,可以删除或添加一些字符,它们各自会消耗价值.问最少消耗多少价值,可以使得字符串变成回文的. [思路] 事实上删除或添加字符的价值只需要保持较小的那一个.假设当前要将(j, ...
- PALIN - The Next Palindrome 对称的数
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
随机推荐
- python RE表达式规则剩余规则
前面我学习了 ’.‘ '^' '$' '*' '+' '?' 基本针对单个字符的,学习python 表达式规则剩余规则. 1,{m} 匹配前一个字符m次 2,{n,.m} 匹配前一个字符n到m次 3 ...
- mysql考试复习
基础创建 字段自动编号auto_increment ( 单词补充:increment 定期的加薪; 增量; 增加) 考点 添加自增 alter table [表名] modify [字段(id)] i ...
- call apply bind的使用方法和区别
call 1.改变this指向 2.执行函数 3.传参 var obj={}; function fun(a,b){ console.log(a,b,this); } fun(1,2); / ...
- Latex--入门系列一
Latex 专业的参考 tex对于论文写作或者其他的一些需要排版的写作来说,还是非常有意义的.我在网上看到这个对于Latex的入门介绍还是比较全面的,Arbitrary reference .所以将会 ...
- C# 列出并删除一个文件夹下的所有MD5值相同的文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 脚本_获取本机 MAC 地址
#!bin/bash#作者:liusingbon#功能:获取本机 MAC 地址ip a s |awk 'BEGIN{print "本机 MAC 地址信息如下:"}/^[0-9]/{ ...
- 1146. Topological Order (25)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- 补比赛——牛客OI周赛9-普及组
比赛地址 A 小Q想撸串 题目分析 普及T1水题惯例.字符串中找子串. Code #include<algorithm> #include<iostream> #include ...
- 装饰器模式-Decerator
一.定义 装饰器模式又叫做包装模式(Wrapper).装饰器模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 在以下情况下应该使用装饰器模式: 1.需要扩展一个类的功能,或给一个类增 ...
- ivew 限制输入 0 到 1 的数字 包括小数, 0 ,1
input <FormItem label="> <Input v-model="formItem.shapeDifferen.breastScaleOutSpa ...