链接:



Palindrome

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2430    Accepted Submission(s): 838

Problem 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
 
Recommend
linle




题意:


先给你字符串的长度 N 
再给你 N 个字符组成的序列,问最小多少个操作【添加一些字符】可以将原来的序列变成回文串

分析:

就是逆序存一下当前序列,然后求出 LCS 【最长公共子序列】
用 N 减去所求的最长公共子序列就可以了

注意到序列很长 5000 如果用裸 dp[5000][5000] 就 MLE 了,
以前在 POJ 上做过这道题, 内存是 hdu  的两倍,看过一篇博客是关于记忆化存储的数组开 dp[maxn][maxn] short int 随便就搞过去了,然后前天晚上就一直 MLE 啊Orz 

完了学妹对我说 %2 开个 dp[2][5000] 就可以过了。。。

也就是前一篇博客的滚动数组思想:

hdu 1159 Common Subsequence 【LCS 基础入门】

当前这一行的状态只由上一行的状态和这一行前面的状态确定,所以只有记录两组Orz
也就是说直接把 裸的 LCS 的数组定义改成 dp[2][maxn], 然后把裸的模板中的 i 全部 %2 就好了
最后输出 n - dp[n%2][n]
关于滚动数组 LCS 如果有不了解的自己去上一篇博客看

hdu 1159 Common Subsequence 【LCS 基础入门】


code:


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 5005;
int dp[2][maxn];
char s1[maxn], s2[maxn]; void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
{
int m1 = dp[(i-1)%2][j];
int m2 = dp[i%2][j-1];
dp[i%2][j] = max(m1, m2);
}
}
}
} int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
scanf("%s", s1);
for(int i = 0; i < n; i++)
s2[i] = s1[n-i-1];
memset(dp,0,sizeof(dp));
LCS(n,n);
printf("%d\n", n-dp[n%2][n]);
}
return 0;
}


最新文章

  1. 人人都是 DBA(VIII)SQL Server 页存储结构
  2. [大坑]FFT学习
  3. POJ1321棋盘问题
  4. cdev_init函数
  5. php中文件引入require
  6. vs2005及以上版本的程序分发问题
  7. probuf了解
  8. AJAX校验商品价格(类似校验用户名)
  9. RHEL6.4安装nginx
  10. 【阿里聚安全&#183;安全周刊】女主换脸人工合成小电影|伊朗间谍APP苹果安卓皆中招
  11. 阿里巴巴是如何打通 CMDB,实现就近访问的?
  12. Linux C 读取文件夹下所有文件(包括子文件夹)的文件名【转】
  13. IEnumerable和IEnumerator使用
  14. 【转】解决Eclipse中SVN版本信息不显示的问题
  15. 【大数据】大数据处理-Lambda架构-Kappa架构
  16. jeecg平台testDatagrid
  17. MVC三级联动无刷新
  18. token 机制
  19. 启动nmon报错while load libncurses.so.5 can not open shared(bit64)
  20. 二十一、IntelliJ IDEA 控制台输出中文乱码问题的解决方法

热门文章

  1. 【Android应用开发技术:用户界面】布局管理器
  2. Normalize.css做了哪些事情--看代码
  3. 并发insert情况下会发生重复的数据插入问题
  4. lantin1
  5. html 基本标签 ---字体
  6. Spring可扩展的XML Schema机制
  7. jquerymobile动态添加元素之后
  8. nginx 为什么要反向代理 影藏后端 高效连接(给nginx,他自己返回) 端口冲突解决 多个服务
  9. Python爬虫学习笔记(一)
  10. HIBERNATE与 MYBATIS的对比,在这里做一下总结