题目链接:

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. Jmeter源代码学习心得

    1.TestPlan和WorkBench GUI类是直接加载的,因此左边的树形菜单开始启动Jmeter时显示也是这两个,默认写死了的!可以改源码!在MenuFactory中有相应代码. 2.其它的GU ...

  2. Dev GridView RowCellClick事件与MouseDown事件

    GridView处于可编辑状态,左键点击默认为“进入编辑”. 将GridView的OptionsColumn.AllowEdit设置为false后左键可触发RowCellClick.但有时候,既希望G ...

  3. sql replace()函数的用法

    replace()函数的用法: replace('带操作的字符串','被换掉的内容'[要换的内容,可写可不写默认为null]) 先上一张图 下面我门对jxid进行操作: select replace( ...

  4. hutool http+天气预报

    中国天气接口:http://www.weather.com.cn/data/sk/地址.html,只显示当天. sojson接口:http://t.weather.sojson.com/api/wea ...

  5. Spyder清除Variable Explorer&&手动安装protobuf3.0(为了配置windows的python接口)

    输入:reset 选择:y PS:建议在windows下,安装anaconda32bit版本的,可以兼容更多第三方包.   Conda使用清华镜像 配置镜像 在conda安装好之后,默认的镜像是官方的 ...

  6. mc03_IntelliJ IDEA配置github

    配置本地git仓库 首先配置一个本地的git仓库,熟悉一下git上传文件到github的过程,具体操作参考 mc02_配置本地git仓库并上传到github IntelliJ IDEA与github的 ...

  7. 转 Python3 错误和异常/ Python学习之错误调试和测试

    ########sample 0 https://www.cnblogs.com/Simon-xm/p/4073028.html except: #捕获所有异常 except: <异常名> ...

  8. Spring MVC自定义错误页面

    在web.xml中添加: <error-page(其他属性404...省略咯)> <location>/error</location> </error-pa ...

  9. Hive学习(三)Hive的Java客户端操作

    Hive的Java客户端操作分为JDBC和Thrifit Client,首先启动Hive远程服务: hive --service hiveserver 一.JDBC 在MyEclipse中首先创建连接 ...

  10. javascript中typeof与instanceof的区别

    JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: typeof typeof 是一个一元运算,放在一个运算数之前 ...