Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 53431   Accepted: 18454

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

Source

IOI 2000
还有一种dp的方法,dp[l][i],表示长度为l的以第i个字符開始的字符串须要多少操作。
对于长度为1的字符串,操作为0
长度为2的字符串,假设两个字符同样,操作为0,不同操作为1
长度为3的字符串。假设左右同样,操作为0。左右不同,对于a[1],a[2],a[3]来说 能够在前面添加a[3],或后面添加a[1],那么就仅仅须要推断剩余的两和字符须要的操作了。

长度为4的字符串,左右同样,那么须要求中间的两个字符,不同的话和长度为3的推断方式同样。
得到 长度为l開始为i的串能够由, 长度为l-1開始为i的,长度为l-1開始为i+1的。或者是长度为l-2,開始为i+1的变化得到。

推出dp公式

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[3][5100] ;
char str[5100] ;
int main()
{
int i , l , k1 , k2 , k3 , n ;
while(scanf("%d", &n) !=EOF)
{
scanf("%s", str);
for(i = n ; i >= 0 ; i--)
str[i] = str[i-1] ;
k1 = -1 ; k2 = 0 ; k3 = 1;
memset(dp,0,sizeof(dp));
for(l = 2 ; l <= n ; l++)
{
k3++ ;
if(k3 == 3) k3 = 0 ;
if(k3 == 0){ k2 = 2 ; k1 = 1 ; }
else if( k3 == 1 ){ k2 = 0 ; k1 = 2 ; }
else { k2 = 1 ; k1 = 0 ; }
for(i = 1 ; i <= n-l+1 ; i++)
{
if( str[i] == str[i+l-1] )
dp[k3][i] = min( min(dp[k2][i]+1,dp[k2][i+1]+1),dp[k1][i+1] ) ;
else
dp[k3][i] = min( dp[k2][i]+1 , dp[k2][i+1]+1);
}
}
printf("%d\n", dp[k3][1]);
}
return 0;
}

1159--Palindrome(dp:回文串变形2)的更多相关文章

  1. poj 3280 Cheapest Palindrome ---(DP 回文串)

    题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...

  2. poj 1159 dp回文串

    题意:添加最少的字符使之成为回文串 #include<cstdio> #include<iostream> #include<algorithm> #include ...

  3. poj3280 Cheapest Palindrome(回文串区间dp)

    https://vjudge.net/problem/POJ-3280 猛刷简单dp第一天第三题. 这个据说是[求字符串通过增减操作变成回文串的最小改动次数]的变体. 首先增减操作的实质是一样的,所以 ...

  4. LightOJ - 1205:Palindromic Numbers (数位DP&回文串)

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  5. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  7. HDU4745--区间DP+回文串

    这题的题意为,给你一个环状的字符串,有两只兔子分别从某任意的石头上开始跳跃.一只顺时针跳.一只逆时针跳.两只兔子每一次落脚处石头的质量都相同.兔子要一步一步的跳,且不能跳到之前跳到过的地方.总的来说, ...

  8. bzoj 3768: spoj 4660 Binary palindrome二进制回文串

    Description 给定k个长度不超过L的01串,求有多少长度为n的01串S满足: 1.该串是回文串 2.该串不存在两个不重叠的子串,在给定的k个串中. 即不存在a<=b<c<= ...

  9. [leetcode]125. Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. 【Python-2.7】换行符和制表符

    在Python中换行符“\n”表示接下来的内容将会换到下一行显示,制表符“\t”表示下面的内容显示时在前面留出空白,如打印如下内容: Dear: I love you forever! 上面的一段话分 ...

  2. setTimeout 0

    setTimeout 0 就是把事件放到下一次事件循环时调用,至少要一个时钟之后: ); console.log('this should before setTimeout 0'); var i=0 ...

  3. Windows离线安装Python第三方库的方法

    在window中,离线安装第三方模块, 1.下载第三方库的压缩文件,解压,将解压后的文件放到Python安装目录下的Lib\site_packages中 2. 将Python添加到环境变量里 3.进入 ...

  4. daxcie

    Database->Edit Current DBMS菜单 修改如下:选中General选项卡,依次打开Script->Sql->Fomat->CaseSensitivityU ...

  5. 并发和多线程(三)--并发容器J.U.C和lock简介

    AQS: 是AbstractQueuedSynchronizer的简称,JUC的核心 底层是sync queue双向链表,还可能有condition queue单向链表,使用Node实现FIFO队列, ...

  6. 参考KOA,5步手写一款粗糙的web框架

    我经常在网上看到类似于KOA VS express的文章,大家都在讨论哪一个好,哪一个更好.作为小白,我真心看不出他两who更胜一筹.我只知道,我只会跟着官方文档的start做一个DEMO,然后我就会 ...

  7. <MyBatis>入门六 动态sql

    package org.maple.mapper; import org.apache.ibatis.annotations.Param; import org.maple.pojo.Employee ...

  8. Ztree加载完成后显示勾选节点

    ①前言:这个在度娘上没有搜到解决的办法,于是自己查看了ztree的API,发现其实还是比较简单的.做个笔记以备不时之需. ②需求: 像下图一样,在加载完成之后就显示需要勾选的项. ③解决方案: 首先页 ...

  9. 通过request对象获取客户端的相关信息

    通过request对象获取客户端的相关信息 制作人:全心全意 通过request对象可以获取客户端的相关信息.例如HTTP报头信息.客户信息提交方式.客户端主机IP地址.端口号等等. request获 ...

  10. CCF201509-2 日期计算 java(100分)

    试题编号: 201509-2 试题名称: 日期计算 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 注意闰年的2月有2 ...