Palindrome
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 53414   Accepted: 18449

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

Source

IOI 2000
题目大意:加入最少的字符。使得字符串变为回文串
假设一个字符串是回文串。那么它与它的逆序数组的最长公共子序列为自身的长度,所以求出最长公共子序列的长度后,用总的长度减去它。得到的就是要改动的长度。

使用short的5000*5000的数组

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
short dp[5100][5100] ;
char str1[5100] , str2[5100] ;
int main()
{
int i , j , n ;
while(scanf("%d", &n) !=EOF)
{
scanf("%s", str1);
for(i = 0 ; i < n ; i++)
str2[n-1-i] = str1[i] ;
str2[i] = '\0' ;
for(i = 1 ; i <= n ; i++)
for(j = 1 ; j <= n ; j++)
{
if( str1[i-1] == str2[j-1] )
dp[i][j] = dp[i-1][j-1]+1 ;
else
dp[i][j] = max( dp[i-1][j],dp[i][j-1] );
}
printf("%d\n", n-dp[n][n]);
}
return 0;
}

使用滚动数组

滚动数组:使用两行数组,模拟大的二维数组

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[2][5100] ;
char str1[5100] , str2[5100] ;
int main()
{
int i , j , n , k ;
while(scanf("%d", &n) !=EOF)
{
scanf("%s", str1);
for(i = 0 ; i < n ; i++)
str2[n-1-i] = str1[i] ;
str2[i] = '\0' ;
k = 0 ;
for(i = 1 ; i <= n ; i++)
{
k = 1 - k ;
for(j = 1 ; j <= n ; j++)
{
if( str1[i-1] == str2[j-1] )
dp[k][j] = dp[1-k][j-1]+1 ;
else
dp[k][j] = max( dp[1-k][j],dp[k][j-1] );
}
}
printf("%d\n", n-dp[k][n]);
}
return 0;
}

poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)的更多相关文章

  1. POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)

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

  2. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

  3. hdu 1080 dp(最长公共子序列变形)

    题意: 输入俩个字符串,怎样变换使其所有字符对和最大.(字符只有'A','C','G','T','-') 其中每对字符对应的值如下: 怎样配使和最大呢. 比如: A G T G A T G -  G ...

  4. hdu1503 最长公共子序列变形

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...

  5. HDU 1513 Palindrome(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...

  6. POJ 2250(最长公共子序列 变形)

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  7. hdu1243(最长公共子序列变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...

  8. hdu1159 dp(最长公共子序列)

    题意:给两个字符串,求这两个字符串的最长公共子序列的长度 因为之前集训的时候做过,所以现在即使会做也并不是什么稀奇的事,依旧为了自己的浅薄感到羞愧啊``` 解法就是通过两个字符串的每个字符互相比较,根 ...

  9. 51Nod 1092 回文字符串 | 最长公共子序列变形

    求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...

随机推荐

  1. perl学习笔记之:正则表达式

     Perl 中的正则表达式 正则表达式的三种形式  正则表达式中的常用模式  正则表达式的 8 大原则          正则表达式是 Perl 语言的一大特色,也是 Perl 程序中的一点难点,不过 ...

  2. Mining of Massive Datasets-1

    given lots of data->discover patterns and models that are: valid, useful, unexpected, understanda ...

  3. I2C驱动框架(三)

    参考:I2C子系统之platform_device初始化——smdk2440_machine_init() I2C驱动框架还应用了另一种总线-设备-驱动模型,平台设备总线platform_bus_ty ...

  4. Untiy CurvedUI 的使用的bug修正

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51996538 作者:car ...

  5. UVa 10534 DP LIS Wavio Sequence

    两边算一下LIS就出来了,因为数据比较大,所以需要二分优化一下. #include <iostream> #include <cstdio> #include <cstr ...

  6. python基础学习笔记——方法返回值

    字符串中(需要有变量接收) 判断是不是阿拉伯数字,返回的是布尔值 1 2 3 4 name = 'alexdasx' new_name = name.isdigit() print(new_name) ...

  7. 使用 RMAN 同步数据库

    使用 RMAN同步数据库 使用 RMAN 同步数据库 一.概述 二 操作步骤 (一).把生产库置为归档模式 (二).启动rman做数据库0级备份 (三).修改生产库数据库到未归档 (四).拷贝备份集到 ...

  8. go 本地安装 grpc-go

    https://blog.csdn.net/code_segment/article/details/77461590 https://github.com/grpc/grpc-go git clon ...

  9. Hibernate框架的主键生成策略

    在Hibernate中,id元素的<generator>子元素用于生成持久化类的对象的唯一标识符,也就是主键.Hibernate框架中定义了许多主键生成策略类,也叫生成器类.所有的生成器类 ...

  10. Spring注解@Component、@Repository、@Service、@Controller

    @Service用于标注业务层组件 @Controller用于标注控制层组件(如struts中的action) @Repository用于标注数据访问组件,即DAO组件 @Component泛指组件, ...