题目链接:

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---变成回文串的最小代价的更多相关文章

  1. poj3280 Cheapest Palindrome(回文串区间dp)

    https://vjudge.net/problem/POJ-3280 猛刷简单dp第一天第三题. 这个据说是[求字符串通过增减操作变成回文串的最小改动次数]的变体. 首先增减操作的实质是一样的,所以 ...

  2. poj 3280 Cheapest Palindrome ---(DP 回文串)

    题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...

  3. POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

    题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...

  4. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  6. [leetcode]125. Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. POJ - 1159 Palindrome(dp-回文变形)

    d.求对字符串最少添加几个字符可变为回文串. s. 法1:直接对它和它的逆序串求最长公共子序列长度len.N-len即为所求.(N为串长度) 因为,要求最少添加几个字符,我们可以先从原串中找到一个最长 ...

  9. bzoj 3768: spoj 4660 Binary palindrome二进制回文串

    Description 给定k个长度不超过L的01串,求有多少长度为n的01串S满足: 1.该串是回文串 2.该串不存在两个不重叠的子串,在给定的k个串中. 即不存在a<=b<c<= ...

随机推荐

  1. Vue 中怎么发起请求(一)

    1.vue 支持开发者引入 jquery 使用 $.ajax() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1.首先,在 package.json 中添加 j ...

  2. Python 元组 (tuple)

    作者博文地址:https://www.cnblogs.com/liu-shuai/ Python的元组与列表类似,同样可通过索引访问,支持异构,任意嵌套.不同之处在于元组的元素不能修改.元组使用小括号 ...

  3. python函数(四)

    一.函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序),在Pasc ...

  4. Kudu的优点

    不多说,直接上干货! Kudu目前具有以下优点  OLAP 工作的快速处理: 与 MapReduce,Spark 和其他 Hadoop 生态系统组件集成: 与 Apache Impala(incuba ...

  5. C语言实现通用链表初步(一)

    注意:本文讨论的是无头单向非循环链表. 假设不采用Linux内核链表的思路,怎样用C语言实现通用链表呢? 一种常用的做法是: typedef int element_t; struct node_in ...

  6. pat04-树8. Complete Binary Search Tree (30)

    04-树8. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHE ...

  7. http request 字段

    Accept: 客户端支持的文件类型, 如果为/表示任何类型 Accept-Encoding: 客户端浏览器支持的文件压缩格式 Accept-Language: 客户端支持的语言 User-Agent ...

  8. WEBAPI测试

    测试 #region 测试 [HttpPost] public HttpResponseMessage UserData(int userId, string userName) { var user ...

  9. dotnetcharting 的简单使用

    dotnetcharting 是一个很好用的图表控件,能画出很漂亮的报表,一般常用到的主要有柱状图.饼图.折线图三种. dotnetcharting 有web版.winform版多个版本可供使用,官方 ...

  10. jQuery 显示与隐藏 tab选项卡

    方法一:使用display样式:block.none来控制文本的显示与隐藏 <div class="explain_text"> 移动互联网为企业提供了连接用户的新方式 ...