给定一个字符串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. [BZOJ2588]Count on a tree(LCA+主席树)

    题面 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个询问 ...

  2. (二:NIO系列) Java NIO Buffer

    出处:Java NIO Buffer Buffer是一个抽象类,位于java.nio包中,主要用作缓冲区.Buffer缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被包装成NIO ...

  3. jquery获取年月日时分秒当前时间

    获取年月日时分秒的当前时间,可按照某种格式显示出来,下面是一种得到如2017年02月02日  00:00:00格式的当前时间 function getCurrentDate(date){ var y ...

  4. timeout使用实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7) - (2/5)

  6. 2018-8-10-sublime-Text-正则替换

    title author date CreateTime categories sublime Text 正则替换 lindexi 2018-08-10 19:16:52 +0800 2018-2-1 ...

  7. idea import class的快捷键

    有自动import class的快捷键 设置如下: 1.alt+enter 2.写好代码之后ctrl+alt+l格式化代码,优化导入包   1.alt+enter 2.写好代码之后ctrl+alt+l ...

  8. Linux–Nginx攻略

    什么是Nginx Nginx (“engine x”) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的Ra ...

  9. [POJ3417]Network(LCA,树上差分)

    Network Description Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried ...

  10. 理解Promise (2)

    一进来 我们开始执行 executor函数 传递两个参数 再调用 then 方法 ,then 方法里面有 OnResolve方法,OnReject 方法  在then 方法中,我们一开始的状态是pen ...