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. Linux 学习(二)

    Linux相关命令 命令 说明 startx 当前用户界面切换至图形界面 init5 切换至另一用户的图形化界面 init3 从图形界面切换回文本界面 pwd 显示当前用户路径 logout 注销 s ...

  2. Spartan6系列之SelectIO深入详解及高级应用简介

    1.      什么是I/O Tile? 对Spartan-6系列FPGA来说,一个IO Tile包括2个IOB.2个ILOGIC.2个OLOGIC.2个IODELAY. 图 1Spartan-6系列 ...

  3. 网站卡测试用 PageSpeed Insights

    这个是google测试网页的;https://developers.google.com/speed/pagespeed/insights/ PageSpeed Insights 简介 PageSpe ...

  4. java_IO_1

    public class DirStudy { public static void main(String[] args) { File file = new File("F:/Eclip ...

  5. 一段简单的手写Java计算器代码

    import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.lang.*; public class Calc ...

  6. iphone6,iphone6 plus适配,旧项目出现黑线问题

    问题:可能开始适配iPhone6和iPhone6 plus的朋友很快就发现,模块器头部和底部会出线一条黑线.但是在其他模拟器完全没有问题.程序也能正常跑.如下图 很清楚的看到头部有一条黑线. 解决办法 ...

  7. 小程序 textarea ios兼容解决

    今天遇到,在小程序里textarea会存在一定的兼容性问题,textarea有默认的内边距,在安卓和ios显示的时候,ios边距会比安卓的大很多. 解决办法: 通过 wx.getSystemInfoS ...

  8. HDU - 4803 - Poor Warehouse Keeper (思维)

    题意: 给出x,y两个值分别代表x个物品,总价为y 有两种变化: 1.使总价+1,数量不变 2.数量+1,总价跟着变化 (y = y + y / x) 思路: 给出目标x,y,计算最少变化次使数量变化 ...

  9. mysql批量替换某个字段的部分内容

    举例说明 有数据表person,结构如下 id name urls 1 张三 xh.jpg 2 李四 xh.jpg 3 王五 3.jpg 需求:将urls字段中的xh替换为id字段的值 语句: UPD ...

  10. 自定义Realm

    [单Realm] 1) jar包 2) 实现自定义Realm public class RealmOne implements Realm{ /** * 获取基本类名 */ @Override pub ...