题目:

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

题意:

输入一个字符串,问最少添加几个字符可以使原字符串成为回文字符串。

思路:

需要加的字符的个数=原来字符串的长度-原来字符串和逆字符串的最长公共子序列的长度。

但是此题中n<=5000,计算最长公共子序列的长度时用int dp[5000][5000]容易超内存,

所以定义dp数组时用short。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
short dp[5005][5005];
int main()
{int n;while(~scanf("%d",&n)){getchar();memset(dp,0,sizeof(dp));char a[5005],b[5005];for(int i=1,j=n;i<=n;j--,i++){scanf("%c",&a[i]);b[j]=a[i];}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(a[i]==b[j])dp[i][j]=dp[i-1][j-1]+1;elsedp[i][j]=max(dp[i-1][j],dp[i][j-1]);}}printf("%d\n",n-dp[n][n]);}return 0;
}

Palindrome相关推荐

  1. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  2. 234. Palindrome Linked List - Easy

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  3. LeetCode 125 Valid Palindrome(有效回文)(*)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5062 ...

  4. Lintcode108 Palindrome Partitioning || solution 题解

    [题目描述] Given a strings, cutsinto some substrings such that every substring is a palindrome. Return t ...

  5. 数位DP 回文序列 POJ-3280 Cheapest Palindrome

    Cheapest Palindrome [ POJ - 3280 ] 题目大意: 给定字符串s,长度为m,由n个小写字母组成.在s的任意位置增删字母,把它变成回文串,增删特定字母的花费不同,求最小花费 ...

  6. Determine whether an integer is a palindrome. Do this without extra space.

    看到这个题目的时候,首先不认识 Determine这个单词.英文不好没办法,查了下是确认的意思,然后不懂 palindrome这个单词, 查了下是回文的意思. 问题是 回文是个什么东西,官方解释: A ...

  7. [leetcode] Palindrome Number

    2019独角兽企业重金招聘Python工程师标准>>> Determine whether an integer is a palindrome. Do this without e ...

  8. Uva - 12050 Palindrome Numbers【数论】

    题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...

  9. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  10. 409 Longest Palindrome

    Longest Palindrome 这题我完全想错了. 求最长回文.~~统计个个字母出现次数.奇数的保留最大的那个.偶数全保留.~~这个思路完全错了. 因为奇数中成对的数字也是可以拿来用的. 正确的 ...

最新文章

  1. Linux 设置core dump
  2. opencv imread
  3. ARM Cortex-A 编程手册学习笔记
  4. 一天教会三岁表弟HTML,你值得拥有
  5. java课程设计进程管理_Java课设总结(个人版)
  6. php微信支付使用ajax,接入微信公众号支付,选择支付方式后,只弹出“error’”(php)...
  7. 机器视觉系统图像采卡基本概念
  8. Spring Cloud云架构 - commonservice-sso服务搭建(一)
  9. Elasticsearch head插件
  10. 剑指offer-数值的整数次方
  11. js生成二维码 中间有logo
  12. Axure 8.1.0.3382 激活码(转)
  13. [对于蓝桥杯要说的一二体会]
  14. Win11右下角音量图标不见了怎么办
  15. pyhanlp 繁简转换,拼音转换与字符正则化
  16. 【obs】libobs-winrt :CreateDispatcherQueueController
  17. java计算抛物线的标准方程_抛物线及其标准方程
  18. linux php cpu,理解Linux CPU负载和 CPU使用率
  19. 2012年度IT博客大赛50强报道:李云
  20. js 高级注释(模块注释,class注释,函数注释等)

热门文章

  1. MySQL的主动优化和被动优化_不要一把梭了,这才是SQL优化的正确姿式!|原创干货...
  2. Xilinx FPGA BRAM使用方法
  3. Android的onDestroy方法,Android:onDestroy()或Application类中的类似方法
  4. 9.Docker Swarm
  5. ubuntu创建、删除文件及目录命令
  6. Hadoop-No.11之元数据
  7. 如何快速构建一个具有可扩展性、弹性伸缩性、安全性、成本低、易用性的无服务器应用?
  8. 结交有识之士-linux
  9. 【100个 Unity实用技能】☀️ | Unity中自定义 2D Sprite 精灵图显示顺序
  10. 千兆交换机VS百兆交换机 哪一款更适合你?