poj 3280 Cheapest Palindrome 区间dp
来源:互联网 发布:c语言return 0的用法 编辑:程序博客网 时间:2024/06/08 03:45
题意:
给添加和删除每个字符的代价,求将一个字符串变成回文串的最小代价。
分析:
区间dp,采用了备忘录方法。
代码:
//poj 3280//sep9#include<iostream>using namespace std;const int maxN=2048;int n,m;int dp[maxN][maxN];char s[maxN];int add_cost[256],del_cost[256];int rec(int x,int y){if(dp[x][y]!=-1)return dp[x][y];if(x>=y)return dp[x][y]=0;if(s[x]==s[y])return dp[x][y]=rec(x+1,y-1);int p=min(add_cost[s[x]],del_cost[s[x]])+rec(x+1,y); int q=min(add_cost[s[y]],del_cost[s[y]])+rec(x,y-1);return dp[x][y]=min(p,q);}int main(){scanf("%d%d",&n,&m);scanf("%s",s);while(n--){char c[8];int a,b;scanf("%s%d%d",c,&a,&b);add_cost[c[0]]=a;del_cost[c[0]]=b;}memset(dp,-1,sizeof(dp));printf("%d\n",rec(0,m-1));return 0;}
0 0
- poj 3280 Cheapest Palindrome(区间DP)
- POJ 3280 Cheapest Palindrome (区间dp)
- poj 3280 Cheapest Palindrome (区间DP)
- poj 3280 Cheapest Palindrome (区间dp)
- poj 3280 Cheapest Palindrome(区间dp)
- Cheapest Palindrome (poj 3280 区间DP)
- [区间dp] poj 3280 Cheapest Palindrome
- poj 3280 Cheapest Palindrome(区间dp)
- POJ - 3280Cheapest Palindrome-经典区间DP
- POJ 3280 Cheapest Palindrome(区间DP)
- POJ 3280 Cheapest Palindrome(区间DP)
- poj 3280 Cheapest Palindrome 区间dp
- poj 3280 Cheapest Palindrome【区间DP】
- POJ 3280 Cheapest Palindrome (区间DP)
- POJ 3280 Cheapest Palindrome(区间DP)
- poj 3280 Cheapest Palindrome 区间dp
- POJ 3280 Cheapest Palindrome(区间dp)
- poj 3280 Cheapest Palindrome 【区间dp】
- JSON.stringify()JSON转字符串 和 JSON.parse()字符串转JSON对象
- 美团2016面试总结
- 教程分享:如何实现Android沉浸式状态栏——教你让你的状态栏变个色!
- Illegal modifier for the variable str; only final is permitted的错误的深入理解
- 抽象类与接口
- poj 3280 Cheapest Palindrome 区间dp
- iOS开发:Iphone的常用控件大全
- 飞机订票系统测试用例
- MySQL下载安装、配置与使用(win7x64)
- Android-Async-HttpClient 源码解析
- Json数据解析
- java面试题二十六 多线程考题
- java的四个访问控制符
- B树、B+树的区别