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

  f[j][i]表示从i这里开始,一直往后j个字符的这一段改成回文串最少需要添加的字符。然后第一维可以拉去滚动。转移是显然的。。

Code

 /**
* poj
* Problem#1159
* Accepted
* Time:782ms
* Memory:748k
*/
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
typedef bool boolean;
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
aFlag = -;
x = getchar();
}
for(u = x - ''; isdigit((x = getchar())); u = u * + x - '');
ungetc(x, stdin);
u *= aFlag;
} template<typename T>class Matrix{
public:
T *p;
int lines;
int rows;
Matrix():p(NULL){ }
Matrix(int rows, int lines):lines(lines), rows(rows){
p = new T[(lines * rows)];
}
T* operator [](int pos){
return (p + pos * lines);
}
};
#define matset(m, i, s) memset((m).p, (i), (s) * (m).lines * (m).rows) int n;
char* str; inline void init() {
readInteger(n);
str = new char[(const int)(n + )];
getchar();
gets(str + );
}
#define idx(i) ((i < 0) ? (i + 3) : (i)) //Matrix<int> f;
int f[][];
inline void solve() {
// f = Matrix<int>(3, n + 1);
for(int i = ; i <= n; i++)
f[][i] = ;
for(int i = ; i < n; i++)
f[][i] = (str[i] == str[i + ]) ? () : ();
int t = ;
for(int k = ; k < n; k++) {
for(int i = ; i + k <= n; i++) {
int j = i + k;
if(str[i] == str[j]) f[t][i] = f[idx(t - )][i + ];
else f[t][i] = min(f[idx(t - )][i], f[idx(t - )][i + ]) + ;
}
t = (t == ) ? () : (t + );
}
printf("%d", f[idx(t - )][]);
} int main() {
init();
solve();
return ;
}

poj 1159 Palindrome - 动态规划的更多相关文章

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

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

  2. 动态规划+滚动数组 -- POJ 1159 Palindrome

    给一字符串,问最少加几个字符能够让它成为回文串. 比方 Ab3bd 最少须要两个字符能够成为回文串 dAb3bAd 思路: 动态规划 DP[i][j] 意味着从 i 到 j 这段字符变为回文串最少要几 ...

  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 (LCS, 滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 55018   Accepted: 19024 Desc ...

  5. poj 1159 Palindrome(dp)

    题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...

  6. POJ 1159 Palindrome(LCS)

    题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...

  7. POJ 1159 Palindrome 最长公共子序列的问题

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  8. poj 1159 Palindrome(区间dp)

    题目链接:http://poj.org/problem?id=1159 思路分析:对该问题的最优子结构与最长回文子序列相同.根据最长回文子序列的状态方程稍加改变就可以得到该问题动态方程. 假设字符串为 ...

  9. POJ 1159 Palindrome(最长公共子序列)

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

随机推荐

  1. mysql的in和not in的用法(特别注意not in结果集中不能有null)

    1. not in的结果集中出现null则查询结果为null; 例如下面sql中,含有list中null值,无法正确查询结果: SELECT COUNT(name) FROM CVE WHERE na ...

  2. keras模型的保存与重新加载

    # 模型保存JSON文件 model_json = model.to_json() with open('model.json', 'w') as file: file.write(model_jso ...

  3. Hadoop书单词

    Hadoop fundamentals :Hadoop原理 英 [ˌfʌndə'mentlz] 美 [ˌfʌndə'mentlz] n.原理; 基本原则,基本法则( fundamental的名词复数) ...

  4. Version Control,Git的下载与安装

    一.什么是Version Control(版本控制系统)?  ——来自百度百科 以Git为例,是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理.Git 是 Linus ...

  5. 关于linux中SSH爆破总结

    2.思路 (1)对root账号进行暴力破解 (2)使用中国姓名top1000作为用户名进行暴力破解 (3)使用top 10000 password字典进行密码破解 (4)利用掌握信息进行社工信息整理并 ...

  6. gispro设置标注属性字体样式设置

    为了应对电子地图和卫星影像的底图,标注样式选择比较关键.挑选了黑字白色晕圈效果.记住不是设置字体轮廓. 因为字体宽度(字粗)有限,设置轮廓直接把字体本身的颜色覆盖了

  7. 《大话设计模式》c++实现 模版方法模式

    模板方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 角色: (1)AbstractClass:是抽象类,其实也 ...

  8. html5-css列表和表格

    td{    /*width: 150px;    height: 60px;*/    padding: 10px;    text-align: center;} table{     width ...

  9. html5-绝对路径/相对路径

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  10. ArrayList 底层实现原理

    ArrayList的底层实现原理 1, 属性:private static final int DEFAULT_CAPACITY = 10; private static final Object [ ...