Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 60290   Accepted: 20998

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
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=;
unsigned short int dp[MAXN][MAXN];//dp[i][j]表示以第i个字母开头,第j个字母结尾的构成回文子串所需添加的字符串
char s[MAXN];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s);
for(int i=;i<n;i++)
{
dp[i][i]=;//有一个字符时
}
for(int i=;i<n;i++)
{
if(s[i-]==s[i])
dp[i-][i]=;
else
dp[i-][i]=;
}
for(int k=;k<n;k++)
{
for(int i=,j=k;j<n;i++,j++)
{
if(s[i]==s[j])
dp[i][j]=dp[i+][j-];
else
dp[i][j]=min(dp[i+][j],dp[i][j-])+;
}
}
printf("%d\n",dp[][n-]);
}
return ;
}

最新文章

  1. java eclipse打jar包和执行jar中的main函数
  2. Redis 存储原理
  3. AbsListView.OnScrollListener 使用注意事项
  4. HTTP请求头host解析
  5. vim highlight whitespace at end of line and auto delete them
  6. Linux远程自动输入密码抓取远程资源
  7. Linux从用户层到内核层系列 - GNU系列之glibc介绍
  8. 转载,find.sh
  9. spring和hibernate的整合
  10. position属性absolute和relative理解
  11. [ZJOI2019]麻将
  12. javaee设计模型简介
  13. OsWatcher 使用详解
  14. 通过Redis、Memcache的 incr 原子操作防刷机制的使用差别
  15. 2.1 mac下多版本jdk的安装和管理
  16. [Java] jdk与jre的区别
  17. c++ 静态类成员函数(static member function) vs 名字空间 (namespace)
  18. 【Shell脚本编程系列】知识储备以及建立规范的脚本
  19. 破解UltraEdit(Ver20.00.0.1040),无限试用
  20. 使用CSS进行定位

热门文章

  1. Hadoop程序基础模板
  2. package.json字段简要解析
  3. Android 下的usb框架及功能点【转】
  4. unbunto关闭触摸屏
  5. multi update caused deadlock problem
  6. ios 发布相关材料记录
  7. BZOJ 1941 kd-tree
  8. CEF3.2623使用记录:windows编译
  9. SQL Server 中WITH (NOLOCK)浅析(转)
  10. IIS7配置PHP简要说明