Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 68562   Accepted: 23869

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

用的LIS的做法 参考1458

字符串s长度为N 将输入的字符串倒过来记做rs 则N - (s与rs的最长公共子序列的长度) 就是答案

用scanf或者getchar()都是1600MS 不知道0MS的是怎么做的

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
const int si = ;
using namespace std;
char s[si], rs[si];
int dp[][si];
int main() {
int N;
cin >> N;
scanf("%s", s);
for (int i = ; i < N; i++) rs[N - i - ] = s[i];
int e = ;
for (int i = ; i <= N; i++) {
for (int j = ; j <= N; j++) {
dp[e][j] = max(dp[ - e][j], dp[e][j - ]);
if (s[i - ] == rs[j - ]) {
dp[e][j] = max(dp[e][j], dp[ - e][j - ] + );
}
}
e = - e;
}
cout << N - dp[ - e][N];
return ;
}

1159 Palindrome的更多相关文章

  1. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

  2. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

  3. OpenJudge/Poj 1159 Palindrome

    1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome T ...

  4. poj 1159 Palindrome - 动态规划

    A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...

  5. poj 1159 Palindrome

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 59094   Accepted: 20528 Desc ...

  6. POJ 1159 - Palindrome (LCS, 滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 55018   Accepted: 19024 Desc ...

  7. poj 1159 Palindrome(dp)

    题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...

  8. POJ 1159 Palindrome(LCS)

    题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...

  9. POJ 1159 Palindrome 最长公共子序列的问题

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  10. poj 1159 Palindrome(区间dp)

    题目链接:http://poj.org/problem?id=1159 思路分析:对该问题的最优子结构与最长回文子序列相同.根据最长回文子序列的状态方程稍加改变就可以得到该问题动态方程. 假设字符串为 ...

随机推荐

  1. Linux 安装vsftpd和ftp客户端

    1.下载安装包:ftp-0.17-54.el6.x86_64.zip和vsftpd-2.2.2-11.el6_4.1.x86_64.zip 可以直接在Linux底下用yum install vsftp ...

  2. Beaglenone读取编码器数据

    一般情况下,beaglebone black默认启动两个cape: 1.BB-BONE-EMMC-2G 2.BB-BONELT-HDMI 我们可以通过编辑uEnv.txt的文件来决定是否启动HDMI, ...

  3. 高度自适应不能触发transition的解决方法

    1. 前言  在我们不能确定一个元素的高度的时候,要使用transition过渡,是不会触发的,比如一个p标签 内容行数不固定  我们可能就要初始 height: 0 ; 过渡到 height: au ...

  4. sublime package control INSTALLATION

    Simple The simplest method of installation is through the Sublime Text console. The console is acces ...

  5. 前后端分离不可缺少的神器 NGINX

    样例讲解 1:安装工具包 wget.vim和gcc yum install -y wget yum install -y vim-enhanced yum install -y make cmake ...

  6. 剑指offer(5)用两个栈实现队列

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 题目分析 栈是先进后出,队列是先进先出,因此两个栈,一个用来push,一个用来pop,同时注意下两个栈不 ...

  7. kvm键盘使用

    在新建导向的时候最后一步之前,选择查看细节那里,在desplay的地方选择VNC server ,再在keyboard地方选择us-en,这下进入安装界面就可以了.

  8. 【Python61--异常处理】

    一.URLrror 当我们的urlopen无法处理一个响应的时候就会出现一个urlerror的错误,但同时url会伴随一个res的属性,包含一个由错误编码和错误信息url 举例: import url ...

  9. Linux查看服务器硬件配置命令

    一.查看服务器硬件信息 dmidecode|grep "System Information" -A9|egrep "Manufacturer|Product|Seria ...

  10. EmailHelper

    注:个人邮箱发送时需要将邮箱密码设置为邮件授权码 邮件发送帮助类一: public class EmailHelper { /// <summary> /// 发送邮件 /// </ ...