POJ-1159 Palindrome---变成回文串的最小代价
题目链接:
https://cn.vjudge.net/problem/POJ-1159
题目大意:
题意很明确,给你一个字符串,可在任意位置添加字符,最少再添加几个字符,可以使这个字符串成为回文字符串。
解题思路:
设原序列S的逆序列为S'
最少需要补充的字母数 = 原序列S的长度 — S和S'的最长公共子序列长度
采用滚动数组节省空间
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 1e4 + ;
char s1[maxn], s2[maxn];
int dp[][maxn], n;
int main()
{
scanf("%d", &n);
scanf("%s", s1);
for(int i = ; i < n; i++)
s2[i] = s1[n - - i];
for(int i = ; i < n; i++)
{
for(int j = ; j < n; j++)
{
int now = (i + ) & ;
if(s1[i] == s2[j])
dp[now][j + ] = dp[!now][j] + ;
else dp[now][j + ] = max(dp[!now][j + ], dp[now][j]);
}
}
cout<<n - dp[n&][n]<<endl;
return ;
}
POJ-1159 Palindrome---变成回文串的最小代价的更多相关文章
- poj3280 Cheapest Palindrome(回文串区间dp)
https://vjudge.net/problem/POJ-3280 猛刷简单dp第一天第三题. 这个据说是[求字符串通过增减操作变成回文串的最小改动次数]的变体. 首先增减操作的实质是一样的,所以 ...
- poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
- POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)
题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 214. Shortest Palindrome 最短回文串
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- POJ - 1159 Palindrome(dp-回文变形)
d.求对字符串最少添加几个字符可变为回文串. s. 法1:直接对它和它的逆序串求最长公共子序列长度len.N-len即为所求.(N为串长度) 因为,要求最少添加几个字符,我们可以先从原串中找到一个最长 ...
- bzoj 3768: spoj 4660 Binary palindrome二进制回文串
Description 给定k个长度不超过L的01串,求有多少长度为n的01串S满足: 1.该串是回文串 2.该串不存在两个不重叠的子串,在给定的k个串中. 即不存在a<=b<c<= ...
随机推荐
- Jenkins自动化CI CD流水线之6--构建邮件状态通知
一. 前提 前提: 服务器开启邮箱服务: 二. 基础配置 需要安装一个插件: 插件: Email Extension Plugin 进行配置: 系统管理->系统设置-> 相关配置如下图: ...
- Asp.net获取系统信息
[DllImport("kernel32")] public static extern void GlobalMemoryStatus(ref MEMORY_INF ...
- Python IDLE快捷键汇总
Python IDLE快捷键汇总 在Options→configure IDLE→keys,查看现存的快捷键,也可以配置选择快捷 编辑状态时: Ctrl+Shift+space(默认与输入法冲突,修改 ...
- Android官方架构组件介绍之LifeCycle(一)
Android官方架构组件介绍之LifeCycle 下面是官方提供的Android App开发的架构图: 从上图可以看到一些关键字:ViewModel,LiveData,Room等.其实看了上面视频的 ...
- auto uninstaller 简体中文版 更新下载地址
地址一(腾讯微云) 地址二(百度网盘) 提取码:3nx7 地址三(直接下载)
- 对象池1(方法功能)PoolOption
2.对象池PoolOption(方法功能) //单类型缓冲对象管理(单模池操作管理)功能: 激活.收回.预加载等. namespace kernal { [System.Serializable] p ...
- [转]jQuery实现图片轮播效果,jQuery实现焦点新闻
本文转自:http://blog.csdn.net/tsyj810883979/article/details/8986157 效果图: 实现代码: <!DOCTYPE html> < ...
- Http Client 源码分析
/** * 此接口仅代表HTTP请求执行的最基本约定. * 它对请求执行过程没有任何限制或特定的细节,并将状态管理.身份验证和重定向处理的细节留给单个实现. */ public interface H ...
- C# WinForm拖入文件到窗体,得到文件路径
private void textBox1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataForma ...
- Actor的一生
Actor应该怎么去形容它呢?它是一段代码扮演的角色.它拥有自己的状态机,能根据外界的消息进行适当的反应.他有记忆能力,可以记住来自外界的多个消息并依次进行反应.Actor就像一个小的生命体,有自己的 ...