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

  1. 5
  2. Ab3bd

Sample Output

  1. 2
  2.  
  3. 这道题类似于编辑距离,使用动态规划可解:
  4.  
  5. dpi,j)表示数组从ij的这个区间形成的子串,使其成为回文串需要加入最小的字符个数
  6.  
  7. 那么动态规划方程为
    1.mindpi+1,j)+1,dp(i,j-1)+1 (a[i]!=a[j])
    dpi,j)={
    2.dp(i+1,j-1) (a[i]=a[j])
  8.  
  9. 这道题由于n最大可达5000,因此使用int型的二维数组会超空间,可以把int型改成short型,同时也可以使用滚动数组,因为这个状态转移方程是在两个相邻的
    状态之间转变,因此使用两行一列的数组就完全可以存下,而且这个DP也类似于递推,由已知推未知,本就用不到那么多的空间,可以覆盖掉那些没用的,只保存有用的就行。
  1. #include"iostream"
  2. #include"cstring"
  3. using namespace std;
  4. const int maxn=5010;
  5. char a[maxn];
  6. int dp[2][maxn],n;
  7. void Init()
  8. {
  9. for(int i=1;i<=n;i++) {cin>>a[i];}
  10. memset(dp,0,sizeof(dp));
  11. }
  12. void Work()
  13. {
  14. for(int i=n-1;i>=1;i--)
  15. {
  16. for(int j=i+1;j<=n;j++)
  17. {
  18. if(a[i]==a[j])
  19. dp[i%2][j]=dp[(i+1)%2][j-1];
  20. else
  21. dp[i%2][j]=min(dp[(i+1)%2][j],dp[i%2][j-1])+1;
  22. }
  23. }
  24. }
  25. void Print()
  26. {
  27. cout<<dp[1][n]<<endl;
  28. }
  29. int main()
  30. {
  31. while(cin>>n)
  32. {
  33. Init();
  34. Work();
  35. Print();
  36. }
  37. return 0;
  38. }
  1.  
  1.  
  1. #include"iostream"
  2. #include"cstring"
  3. using namespace std;
  4.  
  5. const int maxn=;
  6. char a[maxn];
  7. short dp[maxn][maxn],n;
  8.  
  9. void Init()
  10. {
  11.  
  12. for(int i=;i<=n;i++) {cin>>a[i];}
  13. memset(dp,0x3f,sizeof(dp));
  14. }
  15.  
  16. void Work()
  17. {
  18. for(int i=;i<=n;i++) {dp[i][i]=;dp[i+][i]=;}
  19. for(int i=n-;i>=;i--)
  20. {
  21. for(int j=i+;j<=n;j++)
  22. {
  23. if(a[i]==a[j])
  24. dp[i][j]=dp[i+][j-];
  25. else
  26. dp[i][j]=min(dp[i+][j],dp[i][j-])+;
  27. }
  28. }
  29. }
  30.  
  31. void Print()
  32. {
  33. cout<<dp[][n]<<endl;
  34. }
  35.  
  36. int main()
  37. {
  38. while(cin>>n)
  39. {
  40. Init();
  41. Work();
  42. Print();
  43. }
  44. return ;
  45. }
  1.  
  1.  
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. using namespace std;
  5. const int maxn=+;
  6. char a[maxn];
  7. short dp[maxn][maxn];
  8. int main()
  9. {
  10. int n;
  11. while(cin>>n)
  12. {
  13. memset(dp,0x3f,sizeof(dp));
  14. for(int i=;i<=n;i++) cin>>a[i];
  15. for(int i=;i<=n;i++)
  16. {
  17. dp[i-][i]=;
  18. dp[i][i]=;
  19. }
  20. for(int i=;i<=n;i++)
  21. for(int j=i-;j>=;j--)
  22. {
  23. if(a[i]==a[j]) dp[i][j]=dp[i-][j+];
  24. else dp[i][j]=min(dp[i-][j]+,dp[i][j+]+);
  25. }
  26. cout<<dp[n][]<<endl;
  27. }
  28. return ;
  29. }
  1.  
  1.  

集训第五周动态规划 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. 【爬坑系列】之解读kubernetes的认证原理&实践

    对于访问kube-apiserver模块的请求来说,如果是使用http协议,则会顺利进入模块内部得到自己想要的:但是如果是用的是https,则能否进入模块内部获得想要的资源,他会首先要进行https自 ...

  2. vim下撤销操作.选中复制等操作

    vim撤销操作:u vim恢复操作:ctrl+r 使用normal模式下的  v命令,进入visual模式,v+ j/k/h/l   进行文本选中 对于选中的文本进行如下按键: (1.1)d   -- ...

  3. python3 写CSV文件多一个空行的解决办法

    Python文档中有提到: open('eggs.csv', newline='') 也就是说,打开文件的时候多指定一个参数.Python文档中也有这样的示例: import csvwith open ...

  4. 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers

    题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...

  5. JAVA面试题最全集

      JAVA面试题最全集 2009-01-19 15:40 3458人阅读 评论(0) 收藏 举报 java面试ejbservletstringhashmap 一.Java基础知识1.Java有那些基 ...

  6. GIT配置及用法

    ssh配置 TortoiseGit配置 用法: 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓 ...

  7. CSS3实现边框线条动画特效

    <div class="box-line"></div> CSS代码 .box-line, .box-line::before, .box-line::af ...

  8. Rxlifecycle使用详解,解决RxJava内存泄露问题

    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/1122/3711.html

  9. js中cookie的操作

    JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由J ...

  10. UVALive 4128 Steam Roller 蒸汽式压路机(最短路,变形) WA中。。。。。

    题意: 给一个由n*m个正方形格子组成的矩形,其中每个格子的边都是可以走的,长度给定,规定:如果在进入该路前需要拐弯,或者走完该路需要拐弯,都是需要付出双倍距离的(每条路最多算2倍).问从起点到终点的 ...