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. Java内存模型学习笔记

    Java内存模型(JMM):描述了java程序中各种变量(线程共享变量)的范根规则,以及在JVM中将变量存储到内存和从内存中读取出变量这样的底层细节.共享变量就是指一个线程中的变量在其他线程中也是可见 ...

  2. K8S学习笔记之二进制部署Kubernetes v1.13.4 高可用集群

    0x00 概述 本次采用二进制文件方式部署,本文过程写成了更详细更多可选方案的ansible部署方案 https://github.com/zhangguanzhang/Kubernetes-ansi ...

  3. springboot项目打成war包

    在某种情况下,比如..........之下,我们不得不,将springboot打成war包 1.在pom.xml文件中修改 <packaging>war</packaging> ...

  4. 在Linux下OpenCV的下载和编译

    原理上来说,和windows下没有差别,我们同样使用Cmake-GUI来解决问题. 我们推荐QT和OpenCV全部采用官方的方式重新安装一遍,否则可能会丢失一些模块,而这些都会降低开发效率. 1.参考 ...

  5. 复旦大学2016--2017学年第一学期(16级)高等代数I期末考试第七大题解答

    七.(本题10分)  设 $A,B$ 均为 $m\times n$ 阶实矩阵, 满足 $A'B+B'A=0$. 证明: $$r(A+B)\geq\max\{r(A),r(B)\},$$并且等号成立的充 ...

  6. luoguP1850 换教室

    luoguP1850 换教室 链接 https://www.luogu.org/problemnew/show/P1850 思路 状态很显然就是f[n][k][0/1] 前i次,用了k次机会,当前是在 ...

  7. 自制URL转换器

    自定义 url 转换器五个步骤: 定义一个类. 在类中定义一个属性  regex  ,这个属性是用来保存 url 转换器规则的正则表达式. 实现  to_python(self,value)  方法, ...

  8. HTML5的十大新特性

    为了更好地处理今天的互联网应用,HTML5添加了很多新元素及功能,比如: 图形的绘制,多媒体内容,更好的页面结构,更好的形式 处理,和几个api拖放元素,定位,包括网页 应用程序缓存,存储,网络工作者 ...

  9. (转)浏览器对象window,history,location,navigator,screen

    1.window对象:当前的浏览器窗口 window对象是BOM的核心,window对象指当前的浏览器窗口 所有JavaScript全局对象.函数以及变量均自动成为window对象的成员 全局变量是w ...

  10. B站(Bilibili) 视频的下载。

    1) 第一种是众所周知的方法,在URL的 bilibili 前加个 i, 就可以有视频的mp4的地址,然后用下载器下载. 比如 想 下载 https://www.bilibili.com/video/ ...