Description
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.

As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.

Input

Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.

Output

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

Sample Input

5
Ab3bd

Sample Output

2

这道题类似于编辑距离,使用动态规划可解:

用dp(i,j)表示数组从i到j的这个区间形成的子串,使其成为回文串需要加入最小的字符个数

那么动态规划方程为
1.min(dp(i+1,j)+1,dp(i,j-1)+1) (a[i]!=a[j])
dp(i,j)={
2.dp(i+1,j-1) (a[i]=a[j]) 这道题由于n最大可达5000,因此使用int型的二维数组会超空间,可以把int型改成short型,同时也可以使用滚动数组,因为这个状态转移方程是在两个相邻的
状态之间转变,因此使用两行一列的数组就完全可以存下,而且这个DP也类似于递推,由已知推未知,本就用不到那么多的空间,可以覆盖掉那些没用的,只保存有用的就行。
#include"iostream"
#include"cstring"
using namespace std;
const int maxn=5010;
char a[maxn];
int dp[2][maxn],n;
void Init()
{
for(int i=1;i<=n;i++) {cin>>a[i];}
memset(dp,0,sizeof(dp));
}
void Work()
{
for(int i=n-1;i>=1;i--)
{
for(int j=i+1;j<=n;j++)
{
if(a[i]==a[j])
dp[i%2][j]=dp[(i+1)%2][j-1];
else
dp[i%2][j]=min(dp[(i+1)%2][j],dp[i%2][j-1])+1;
}
}
}
void Print()
{
cout<<dp[1][n]<<endl;
}
int main()
{
while(cin>>n)
{
Init();
Work();
Print();
}
return 0;
}

 
#include"iostream"
#include"cstring"
using namespace std; const int maxn=;
char a[maxn];
short dp[maxn][maxn],n; void Init()
{ for(int i=;i<=n;i++) {cin>>a[i];}
memset(dp,0x3f,sizeof(dp));
} void Work()
{
for(int i=;i<=n;i++) {dp[i][i]=;dp[i+][i]=;}
for(int i=n-;i>=;i--)
{
for(int j=i+;j<=n;j++)
{
if(a[i]==a[j])
dp[i][j]=dp[i+][j-];
else
dp[i][j]=min(dp[i+][j],dp[i][j-])+;
}
}
} void Print()
{
cout<<dp[][n]<<endl;
} int main()
{
while(cin>>n)
{
Init();
Work();
Print();
}
return ;
}

 
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=+;
char a[maxn];
short dp[maxn][maxn];
int main()
{
int n;
while(cin>>n)
{
memset(dp,0x3f,sizeof(dp));
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++)
{
dp[i-][i]=;
dp[i][i]=;
}
for(int i=;i<=n;i++)
for(int j=i-;j>=;j--)
{
if(a[i]==a[j]) dp[i][j]=dp[i-][j+];
else dp[i][j]=min(dp[i-][j]+,dp[i][j+]+);
}
cout<<dp[n][]<<endl;
}
return ;
}


集训第五周动态规划 G题 回文串的更多相关文章

  1. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  2. 集训第五周动态规划 D题 LCS

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  3. 集训第五周动态规划 C题 编辑距离

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  4. 集训第五周 动态规划 B题LIS

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Des ...

  5. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  6. 集训第五周动态规划 F题 最大子矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  7. 集训第五周 动态规划 K题 背包

    K - 背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  8. 集训第五周动态规划 J题 括号匹配

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  9. 集训第五周动态规划 E题 LIS

    Description The world financial crisis is quite a subject. Some people are more relaxed while others ...

随机推荐

  1. 51nod 1068 Bash游戏 V3

    列出前几项可以发现是个规律题,不要被题目的文字所欺骗,字符串处理10^1000即可 #include <bits/stdc++.h> using namespace std; int ge ...

  2. 计算机视觉-SIFT特征匹配进行目标转换

    Lowe将SIFT算法分解为如下四步: 1. 尺度空间极值检测:搜索所有尺度上的图像位置.通过高斯微分函数来识别潜在的对于尺度和旋转不变的兴趣点. 关键点定位:在每个候选的位置上,通过一个拟合精细的模 ...

  3. 递推DP UVA 607 Scheduling Lectures

    题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按 ...

  4. 题解报告:hdu 1228 A+B(字符串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1228 Problem Description 读入两个小于100的正整数A和B,计算A+B. 需要注意 ...

  5. 如何用PS快速做出3D按钮效果的图片

    1 先建立一个透明图层 2:再创建一个矩形 3:选用过喷样式 4: 双击图层并应用蓝色,记得这里应该复制下颜色的16进制值. 效果如图所示 取消光泽选项,大功告成! 最终效果如图所示,将其保存为PNG ...

  6. UML 顺序图(转载)

    顺序图精确表达用户与系统的复杂交互过程. 顺序图用于描述进出系统的信息流. 顺序图与协作图是同构的,可以互相转换!!! 顺序图:着重体现对象间消息传递的时间顺序.顺序图允许直观的表示出对象的生存期,生 ...

  7. 移动端UI自动化Appium测试——Windows系统Appium环境配置

    1.安装JDK,官网下载即可,这里用的1.8,环境变量配置 2.安装Android sdk,API >= 17,环境变量配置 3.安装Nodejs,官网http://nodejs.org/dow ...

  8. Docker Java+Tomcat 环境搭建

    软件环境:jdk.tomcat.docker.centos.虚拟机 首先,您要准备一个 CentOS 的操作系统,虚拟机也行.总之,可以通过 Linux 客户端工具访问到 CentOS 操作系统就行. ...

  9. Android Studio中找出不再使用的资源

    顶部Analyze菜单中选择Run Inspection by Name 在弹出的输入框中输入unused resources

  10. php防止页面刷新代码

    //代理IP直接退出 empty($_SERVER['HTTP_VIA']) or exit('Access Denied'); //防止快速刷新 session_start(); $seconds ...