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. RN调试

    https://facebook.github.io/react-native/docs/debugging.html 热加载 RN的目标是极致的开发体验,修改文件后能在1秒内看到变化,通过以下三个特 ...

  2. vuex其实超简单,只需3步

    前言 之前几个项目中,都多多少少碰到一些组件之间需要通信的地方,而因为种种原因,event bus 的成本反而比vuex还高, 所以技术选型上选用了 vuex, 但是不知道为什么,团队里的一些新人一听 ...

  3. 爬虫练习四:爬取b站番剧字幕

    由于个人经常在空闲时间在b站看些小视频欢乐一下,这次就想到了爬取b站视频的弹幕. 这里就以番剧<我的妹妹不可能那么可爱>第一季为例,抓取这一番剧每一话对应的弹幕. 1. 分析页面 这部番剧 ...

  4. 基础训练 2n皇后问题

    2n皇后问题 #include<iostream> #include<vector> using namespace std; int cnt = 0, n; vector&l ...

  5. 基于链式链表的栈链式存储的C风格实现

    链式链表的头文件与CPP文件见前文 头文件: #ifndef _LINKSTACK_H_ #define _LINKSTACK_H_ typedef void LinkStack; //创建一个栈 L ...

  6. Exchange 正版化 授权

    网友说法: Exchange服务器版其实价格不高,企业版也就是几万左右,贵的是客户端授权,一个客户端授权大概要300多.但是,但是,中国企业买Exchange客户端一般都是可以按比例买的,比如10%- ...

  7. 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)

    这是LeetCode里的第25道题. 题目要求: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最 ...

  8. HDU 3341 Lost's revenge

    Lost's revenge Time Limit: 5000ms Memory Limit: 65535KB This problem will be judged on HDU. Original ...

  9. POJ 1543 Perfect Cubes

    Perfect Cubes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12595   Accepted: 6707 De ...

  10. nodemailer发送邮件遇到的一些问题

    使用nodemailer发送邮件一直困惑了我好几天,百度谷歌都没有找到的几个问题,方便大家排查. 我使用的是0.7.1版本,其他的版本好像报错,就没用. 错误信息:Mail from command ...