给定一个字符串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. 《剑指offer》面试题19 二叉树的镜像 Java版

    书中方法:这道题目可能拿到手没有思路,我们可以在纸上画出简单的二叉树来找到规律.最后我们发现,镜像的实质是对于二叉树的所有节点,交换其左右子节点.搞清楚获得镜像的方法,这道题实际上就变成了一道二叉树遍 ...

  2. GCD and LCM HDU 4497 数论

    GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...

  3. 埃及分数问题(带乐观估计函数的迭代加深搜索算法-IDA*)

    #10022. 「一本通 1.3 练习 1」埃及分数 [题目描述] 在古埃及,人们使用单位分数的和(形如 $\dfrac{1}{a}​$​​ 的,$a$ 是自然数)表示一切有理数.如:$\dfrac{ ...

  4. LED音乐频谱之点阵

    转载请注明出处:http://blog.csdn.net/ruoyunliufeng/article/details/37967455 一.硬件 watermark/2/text/aHR0cDovL2 ...

  5. [ASP.NET Core 3框架揭秘] 依赖注入:IoC模式

    原文:[ASP.NET Core 3框架揭秘] 依赖注入:IoC模式 正如我们在<依赖注入:控制反转>提到过的,很多人将IoC理解为一种“面向对象的设计模式”,实际上IoC不仅与面向对象没 ...

  6. .NET平台 C# ASP.NET

    .NET 平台 根据微软的定义: .NET is a“ revolutionary new platform, built on open Internet protocols and standar ...

  7. log4j常用的配置文件

    # priority :debug<info<warn<error #you cannot specify every priority with different file fo ...

  8. 求助:关于shell数值比较的错误提示

    今天写了个脚本,过不了错误这一关,求大神路过瞟一眼. 1 #!/bin/bash 2 #disk use 3 disk_use() { 4 DISK_LOG=/tmp/disk_use.tmp 5 D ...

  9. 好玩的Linux命令-1

    Ag:比grep.ack更快的归递搜索文件内容 1:首先在linux创建个sh文件->ag.sh 2:在ag.sh里面输入如下内容并保存 #!/bin/bash set -x TEMP_DIR= ...

  10. 利用C51单片机模拟SPI进行双机通信

    SPI协议简述 SPI,是英语Serial Peripheral interface的缩写,顾名思义就是串行外围设备接口.由Motorola首创.SPI接口主要应用在 EEPROM,FLASH,实时时 ...