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. sqlserver 空 处理

    NULL:没有对(列)变量输入数据 'NULL'字符串:是长度为4的字符串 空字符串:有对(列)变量输入数据,输入的数据为空字符串 select case lxdh when '' then '空' ...

  2. Oracle之with as和update用法

    许久不用,又忘了,做个记录 update test b set b.code=(with t as(select t.id,code||'_'||row_number() over(partition ...

  3. Linux系统组成

     1 系统组成 BootLoader:操作系统引导程序 内核: 文件系统:应用程序(用户开发的.网上下载的) 2 安装USB驱动 dongry@d-linux:~$ insmod usb_dnw.ko ...

  4. Assembly Required【思维】

    问题 A: Assembly Required 时间限制: 1 Sec  内存限制: 128 MB 提交: 49  解决: 25 [提交] [状态] [命题人:admin] 题目描述 Princess ...

  5. freeswitch编译安装,初探, 以及联合sipgateway, webrtc server的使用场景。

    本文主要记录freeswitch学习过程. 一 安装freeswitch NOTE 以下两种安装方式,再安装的过程中遇到了不少问题,印象比较深刻的就是lua库找到不到这个问题.这个问题发生在make ...

  6. JAVA深入研究——Method的Invoke方法(转)

    原文地址:http://www.cnblogs.com/onlywujun/p/3519037.html 在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一 ...

  7. Vs10.设置.高亮(20190327)

    ZC:(20190327)只要使用的是 "Highlight all occurrences of selected word" 和 "Visual Assist X&q ...

  8. 盛最多水的容器(java实现)

    题目: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的 ...

  9. Series.str——字符串批量处理方法

    针对dataframe中的某一行(或列)想做批量字符串处理时,可采用此方法 series.str.python内置的str方法 例如: series.str.replace('A','B')   # ...

  10. 基于RobotFramework——自定义kafka库并导入使用

    [Kafka] 首先介绍一下我了解的kafka的皮毛信息—— kafka——一个分布流处理系统:流处理:可以像消息队列一样publish或者subscribe信息:分布式:提供了容错性,并发处理消息的 ...