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. 2019/4/2 wen 多态、抽象

  2. Linux NFS挂载

    Linux NFS挂载 一.NFS挂载 192.25.10.101/home/sharedata/azkaban/ODS_HS08 挂载到 192.25.10.102/home/data_azkaba ...

  3. 分类统计的controller和service

    SpringMVC框架下的 部分代码: Controller控制器: @Resource ReviewTitleService reviewTitleService;//调用ReviewTitleSe ...

  4. Spring错误——Spring 注解——factory-bean reference points back to the same bean definition

    背景:学习Spring,在使用注解@Bean的name属性配置<bean>实例时,不能注册实例成功 报错 WARNING: Exception encountered during con ...

  5. T-net 【贪心】

    问题 H: T-net 时间限制: 1 Sec  内存限制: 128 MB 提交: 302  解决: 14 [提交] [状态] [命题人:admin] 题目描述 T-net which is a ne ...

  6. loj#2483. 「CEOI2017」Building Bridges 斜率优化 cdq分治

    loj#2483. 「CEOI2017」Building Bridges 链接 https://loj.ac/problem/2483 思路 \[f[i]=f[j]+(h[i]-h[j])^2+(su ...

  7. centos6.5下安装Nginx

    链接: https://www.jb51.net/article/118595.htm

  8. 【Entity Framework】Model First Approach

    EF中的model first 所谓mf, 就是使用vs提供的edm designer去设计model,然后将设计好的model使用vs在指定的数据库中生成数据库即可. 当你的项目既没有数据库也没有c ...

  9. js弹出对话框的三种方式(转)

    原文地址:https://www.jb51.net/article/81376.htm javascript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prom ...

  10. python学习-----协程

    一.协程的引入 对于单线程下,我们不可避免程序中出现io操作,但如果我们能在自己的程序中(即用户程序级别,而非操作系统级别)控制单线程下的多个任务能在一个任务遇到io阻塞时就切换到另外一个任务去计算, ...