poj 3280 回文字符串问题 dp算法
题意:给一个字符串,构成回文(空也是回文) 其中增删都需要代价。问:代价最少?
思路:把字符串s变空 dp[i][j]表示变成回文的最小代价
for(i=m-1;i>=0;--i)
for(j=i+1;i<w;j++)lsdjfl
dp[i][j]=min(dp[i+1][j]+cost[s[i]-'a']],dp[i][j-1]+cost[s[j]-'a'])
if(s[i]==s[j]) dp[i][j]=min(dp[i][j],dp[i+1][j-1])
其中cost表示增删中最小代价
解决问题的代码:
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int dp[][];
int cost[];
int main()
{
int n, m;
cin >> n >> m;
string s;
cin >> s;
for (int i = ; i < n; i++)
{
int add, del;
char c;
cin >> c >> add >> del;
cost[c - 'a'] = min(add, del);
}
for(int i=m-;i>=;--i)
for (int j = i + ; j < m; j++)
{
dp[i][j] = min(dp[i + ][j] + cost[s[i] - 'a'], dp[i][j - ] + cost[s[j] - 'a']);
if (s[i] == s[j])
dp[i][j] = min(dp[i][j], dp[i + ][j - ]);
}
printf("%d\n", dp[][m - ]);
return ;
}
poj 3280 回文字符串问题 dp算法的更多相关文章
- nyoj 37 回文字符串 【DP】
先反向复制一个新的字符串,然后再找出最长公共子串,在用长度n减去就可以 回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 所谓回文字符串,就是一个字符串,从 ...
- Manacher算法:求解最长回文字符串,时间复杂度为O(N)
原文转载自:http://blog.csdn.net/yzl_rex/article/details/7908259 回文串定义:"回文串"是一个正读和反读都一样的字符串,比如&q ...
- leetcode 730. 统计不同回文子序列(区间dp,字符串)
题目链接 https://leetcode-cn.com/problems/count-different-palindromic-subsequences/ 题意 给定一个字符串,判断这个字符串中所 ...
- 计算字符串的最长回文子串 :Manacher算法介绍
转自: http://www.open-open.com/lib/view/open1419150233417.html Manacher算法 在介绍算法之前,首先介绍一下什么是回文串,所谓回文串,简 ...
- 最长回文字符串(manacher算法)
偶然看见了人家的博客发现这么一个问题,研究了一下午, 才发现其中的奥妙.Stupid. 题目描述: 回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串. ...
- 第5题 查找字符串中的最长回文字符串---Manacher算法
转载:https://www.felix021.com/blog/read.php?2040 首先用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度:在每个字符的两边都插入一 ...
- hiho 1323 : 回文字符串 dp
#1323 : 回文字符串 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个字符串 S ,最少需要几次增删改操作可以把 S 变成一个回文字符串? 一次操作可以在任 ...
- hihocoder 1323 回文字符串(字符串+dp)
题解: 比较水的题目 dp[i][j]表示[i...j]最少改变几次变成回文字符串 那么有三种转移 dp[i][j] = dp[i+1][j-1] + s[i] != s[j] dp[i][j] = ...
- hdu3068 求一个字符串中最长回文字符串的长度 Manacher算法
最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- Maven--setting详解
settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. settings.xml文件是干什么的,为什么要配置它呢? ...
- Mvc异常处理器
using System; using System.Text; using EMS.Domains.Core; using System.Web.Mvc; using Json.Net; using ...
- c# 串口操作
public class CommPort : IDisposable { public string Port = ""; ///<summary> ///波特率96 ...
- 关于 SQL Server Reporting Services 匿名登录的解决方案
每次访问报表都需要windows验证,这样的报表给客户确实很说不过去. SSRS 可以匿名登录的设定步骤: 环境: 开发工具:SQL Server Business Intelligence Deve ...
- spring4、hibernate4整合xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- SqlServer 填充因子的说明
CREATE NONCLUSTERED INDEX IX_d_name ON department(d_name) with fillfactor=30 使用 fill factor 选项可以指定 M ...
- MovieReview—Kingsman THE SECRET SERVICE(王牌特工之特工学院)
Brain Storming Mr. Valentine's Day see excess human beings as the Earth's virus and try to e ...
- ASUS主板 Type C 接口无效问题
修改UEFI设置,把 USB TYPE C POWER SWITCH 改成启用
- Problem N: 求二维数组中的鞍点【数组】
Problem N: 求二维数组中的鞍点[数组] Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2764 Solved: 1728[Submit][S ...
- overloading and overriding
What is the difference between method overloading and method overriding in Java? Differences between ...