Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 62102   Accepted: 21643

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

本题是一个求LCS长度的问题,具体算法及思路可以参考

http://blog.csdn.net/v_july_v/article/details/6695482

注意用int会爆空间

#include<iostream>
using namespace std; short c[5005][5005];
char s[5005]; int main()
{
int n;
cin>>n; for(int i=1;i<=n;i++)
{
cin>>s[i];
} for(int i=0;i<=n;i++)
c[i][0]=c[0][i]=0; for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(s[i]==s[n-j+1])
c[i][j]=c[i-1][j-1]+1;
else if(c[i-1][j]>=c[i][j-1])
c[i][j]=c[i-1][j];
else
c[i][j]=c[i][j-1];
} cout<<n-c[n][n]<<endl; return 0;
}

  

[POJ] Palindrome的更多相关文章

  1. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

  2. POJ 3974 Palindrome

    D - Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  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(最长公共子序列)

    Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...

  5. POJ 3974 - Palindrome - [字符串hash+二分]

    题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...

  6. poj 1159 Palindrome - 动态规划

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

  7. 【POJ 1159】Palindrome

    [POJ 1159]Palindrome 近期各种题各种奇葩思路已经司空见惯了...又新出个滚动数组= = 该题另一点须要知道 最少须要补充的字母数 = 原序列S的长度 - S和S'的最长公共子串长度 ...

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

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

  9. poj 3280 Cheapest Palindrome

    链接:http://poj.org/problem?id=3280 思路:题目给出n种m个字符,每个字符都有对应的添加和删除的代价,求出构成最小回文串的代价 dp[i][j]代表区间i到区间j成为回文 ...

随机推荐

  1. 安卓开发 报错 错误:This version of android studio is incompatible with the gradle version used. 的解决

    本文的解决方法主要参考以下文章: https://blog.csdn.net/sinat_15417921/article/details/51907728 Android 开发总是会遇到各种不知道怎 ...

  2. tcp流式套接字和udp数据报套接字编程区别

    1. 流式套接字采用字节流方式进行传输,而数据报套接字 使用数据报形式传输数据2. tcp套接字会产生粘包,udp有消息边界,不会形成粘包.3. tcp编程可以保证消息的完整性,udp则不能保证4. ...

  3. MySQL 5.7笔记

    1. 初始化 重命名my-default.ini为my.ini 添加character_set_server=utf8 运行mysqld --initialize 2. 重置密码 服务器运行: mys ...

  4. 机器学习中的ground truth

    ground truth就是参考标准,一般用来做误差量化.比方说要根据历史数据预测某一时间的温度,ground truth就是那个时间的真实温度.error就是(predicted temperatu ...

  5. websocket IE11 提示SecurityError

    一开始我从网上下载的示例地上localhost:8081 在其它 浏览器下都正常,就在IE11下提示SecurityError, 然后把我把localhost换成IP,就好了. 依次记录下.

  6. HDU4612Warm up 边双连通 Tarjan缩点

    N planets are connected by M bidirectional channels that allow instant transportation. It's always p ...

  7. js判断当前浏览类型

    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串var isOpera = userAgent.indexOf("Opera ...

  8. LOJ2323. 「清华集训 2017」小 Y 和地铁 【搜索】【思维】【好】

    LINK 思路 首先如果直接算每一个段有三个决策 左/右 上/下 跨不跨过端点 这样的复杂度是\((2^3)^{22}\),显然是无法接受的 然后考虑怎么优化这个东西 首先左右这个决策是没有意义的 因 ...

  9. LOJ2422 NOIP2015 斗地主 【搜索+贪心】*

    LOJ2422 NOIP2015 斗地主 LINK 题目大意很简单,就是问你斗地主的一分手牌最少多少次出完 然后我们发现对于一种手牌状态,不考虑顺子的情况是可以贪心做掉的 然后我们直接枚举一下顺子出牌 ...

  10. Codeforces 17E Palisection 【Manacher】

    Codeforces 17E Palisection E. Palisection In an English class Nick had nothing to do at all, and rem ...