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. django开发(二)

    1.django数据库操作---model的使用以及django自带的数据api django中已经做了ORM,表就是一个类class,表中的一个项就是一个对象object,很好用 1.1django ...

  2. window安装ab压力测试并使用

    ab是Apache HTTP server benchmarking tool的缩写,可以用以测试HTTP请求的服务器性能,也是业界比较流行和简单易用的一种压力测试工具包 1.下载ab工具 进入apa ...

  3. Sanic

    基础 厉害了我的 Sanic hello word, Sanic

  4. Linux 基础内容

    1.linux版本有:redhat(收费),centos,ubuntu,suse(开发使用) 2./目录下的:etc配置文件目录,media挂载点,opt第三方安装目录,boot启动文件,home家, ...

  5. Container的简单认识

    容器是一个标准的软件单元,它将代码及其所有依赖关系打包,以便应用程序从一个计算环境快速可靠地运行到另一个计算环境. Docker容器映像是一个轻量级,独立的可执行软件包,包含运行应用程序所需的一切:代 ...

  6. CSS【06】:CSS显示模式

    div和Span标签 作用:一般用于配合css完成网页的基本布局 <style> .header{ width: 980px; height: 100px; background: red ...

  7. Docker Doc之一:小白入门

    Docker与虚拟机相比有更大的优势,而且微软巨硬全力支持Docker,这个不得不让大家关心一下Docker的3W. Docker是什么 2013 年初,PaaS 提供商 dotCloud 开源了一个 ...

  8. FZOJ P2109 【卡德加的兔子】

    题目描述 卡德加喜欢养兔子.他在达拉然的下水道里放了 $N$ 个兔笼(编号从 $1$ 到 $N$),里面养着他从德拉诺带来的兔子.它们的繁殖遵循斐波那契数列的规律:刚开始时,笼子里有一对刚出生的兔子. ...

  9. Vue:(四)Ajax(Vue-Resource)

    Vue 要实现异步加载需要使用到 vue-resource 库.(挂载到vue实例上) (一)Vue-Resource引入 <script src="https://cdn.stati ...

  10. OpenGL.Tutorial16_ShadowMapping

    1. 2. In Tutorial 15 we learnt how to create lightmaps, which encompasses(包含) static lighting. While ...