给定一个字符串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的更多相关文章

  1. poj3280Cheapest Palindrome(记忆化)

    链接 真的1A了.. 一开始想复杂了 想着补全再删 没想好 后来想到递归 大的回文串是由小的推过来的 一直递归下去 对于当前的i,j可以选择保留或者删除 选个最小的 #include <iost ...

  2. POJ3280--Cheapest Palindrome(动态规划)

    Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate ...

  3. 【动态规划】POJ3280- Cheapest Palindrome

    [题目大意] 给出一个字符串,可以删除或添加一些字符,它们各自会消耗价值.问最少消耗多少价值,可以使得字符串变成回文的. [思路] 事实上删除或添加字符的价值只需要保持较小的那一个.假设当前要将(j, ...

  4. PALIN - The Next Palindrome 对称的数

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  5. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  6. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  7. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  8. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  9. [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 ...

随机推荐

  1. JS—图片压缩上传(单张)

    *vue+webpack环境,这里的that指到vue实例 <input type="file" name="file" accept="ima ...

  2. 【推荐系统】知乎live入门

    参考链接: 知乎推荐系统live:姚凯飞推荐系统live 目录 1.推荐概览与框架 2.细节补充 3.召回 4.排序 5.常用技能与日常工作 5.用户画像-特征工程 6.相关经验 7.推荐考试拿分路径 ...

  3. Oracle 11g+Windows10 x64安装、配置过程记录

    备注:本想在自己电脑上安装个oracle练习用,但是害怕安装过程中出现问题,而oracle的卸载又是出了名的麻烦,所以用虚拟机搭建了一个跟本机一样的系统,同时记录下安装的每一步. 环境:windows ...

  4. 2018-2-13-win10-uwp-读写csv-

    title author date CreateTime categories win10 uwp 读写csv lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17 ...

  5. dotnet ef执行报错, VS 2019发布时配置项中的Entity Framework迁移项显示不出来

    VS 2019发布时配置项中的Entity Framework迁移项显示不出来 dotnet ef dbcontext list --json “无法执行,因为找不到指定的命令或文件.可能的原因包括: ...

  6. OGG-01161

    Bad column index (35) specified for table user.table_name, max columns = 35. 原因:源端表结构发生了变更 解决办法:1.如果 ...

  7. unittest单元测试框架总结(转)

    unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果.今天笔者 ...

  8. 1085. PAT单位排行 (25)

    每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数N(<=105),即考生人数.随后N行,每行按下列格式给出一个考生的信 ...

  9. Angular Viewchild undefined

    Angular的viewchild在使用的时候报错 undefined 1 检查是否在元素上打上标识 #xxx 2 查看引用元素时的时机 是否在AfterViewInit之后 3 检查元素是否在*ng ...

  10. python字符串前面的u,还有r

    以u或U开头的字符串表示unicode字符串 如果你想要用非英语写文本,那么你需要有一个支持Unicode的编辑器.(了解一下unicode和ascll码还有utf-8) u'你好'        # ...