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. 封装及调用fetch

    一.封装fetch 创建fetch/index.js import 'whatwg-fetch' import 'es6-promise' export function get(url) { let ...

  2. Javascript-全局函数和局部函数作用域的理解

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

  3. 如何简单地理解Python中的if __name__ == '__main__'(https://blog.csdn.net/yjk13703623757/article/details/77918633)

    1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__name__ == '小明'):在你自己眼中,你是你自己(__name__ == '_ ...

  4. SQL Server 创建索引(index)

    索引的简介: 索引分为聚集索引和非聚集索引,数据库中的索引类似于一本书的目录,在一本书中通过目录可以快速找到你想要的信息,而不需要读完全书. 索引主要目的是提高了SQL Server系统的性能,加快数 ...

  5. JDBC连接自定义sqlserver数据库实例名(多个实例)

    java语言中,通过jdbc访问sqlserver2005(2008)数据库默认实例可以按常用的写法来写url连接.代码如下: <span style="font-size:12px; ...

  6. Python记录3:集合

    #一,集合类型###找出两个集合的交集并将结果打印出来# python_stu=['egon','alex','kevin','王大炮','李二丫']# linux_stu=['张铁蛋','刘铜蛋', ...

  7. 向量体系结构(2)----SIMD指令集扩展和GPU

    进行SIMD多媒体扩展的设计,源于一个很容易观察到的事实: 许多多媒体应用程序操作的数据类型比对32位处理器进行针对性优化的数据类型更窄一些. 图像三基色,都是8位.音频采样也都是8位和16位来表示. ...

  8. [14]Windows内核情景分析 --- 文件系统

    文件系统 一台机器上可以安装很多物理介质来存放资料(如磁盘.光盘.软盘.U盘等).各种物理介质千差万别,都配备有各自的驱动程序,为了统一地访问这些物理介质,windows设计了文件系统机制.应用程序要 ...

  9. SQLGetStmtAttr

    SQLGetStmtAttr 函数定义: SQLRETURN SQLGetStmtAttr( SQLHSTMT     StatementHandle, SQLINTEGER     Attribut ...

  10. wxpython(python3.5)安装

    安装步骤: http://blog.csdn.net/xiaodong193/article/details/51920283 注意:安装软件前需要阅读其中的README.txt,可快速知道安装方法, ...