题意:给出一个字符串s,问至少加入多少个字母让它变成回文串

解题思路:求出该字符串与该字符串翻转后的最长公共子序列的长度,再用该字符串的长度减去最长公共子序列的长度即为所求

反思:因为题目所给的n的范围为3<=n<=5000,所以dp[][]数组如果开到dp[5005][5005],会超内存,此时应该就用滚动数组来优化

滚动数组的详细介绍http://blog.csdn.net/niushuai666/article/details/6677982

Palindrome

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3301    Accepted Submission(s): 1140

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
 
#include<stdio.h>
#include<string.h>
char s[5005],w[5005];
int dp[2][5005];
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int main()
{
int n,i,j,x,y;
while(scanf("%d",&n)==1)
{
scanf("%s",&s);
for(i=0;i<n;i++)
w[i]=s[n-1-i];
w[i]='\0';
memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++)
{ for(j=1;j<=n;j++)
{
x=i%2;
y=1-x;
if(s[i-1]==w[j-1])
dp[x][j]=dp[y][j-1]+1;
else
dp[x][j]=max(dp[y][j],dp[x][j-1]);
}
}
printf("%d\n",n-dp[n%2][n]);
}
}

  

 

HDU 1513 Palindrome【LCS】的更多相关文章

  1. hdu 1513 Palindrome【LCS滚动数组】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1513 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. hdoj 1513 Palindrome【LCS+滚动数组】

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

  3. HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...

  4. poj 1159 Palindrome 【LCS】

    任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <io ...

  5. 125. Valid Palindrome【easy】

    125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...

  6. HDU 5963 朋友 【博弈论】 (2016年中国大学生程序设计竞赛(合肥))

    朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Descr ...

  7. HDU 5965 扫雷 【模拟】 (2016年中国大学生程序设计竞赛(合肥))

    扫雷 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...

  8. HDU 5935 Car 【模拟】 (2016年中国大学生程序设计竞赛(杭州))

    Car Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  9. HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

随机推荐

  1. ZBrush中的PolyPainting如何理解?

    什么是PolyPainting? PolyPainting在ZBrush ®中是一种创建纹理的方法,该方法通过对每个多边形顶点应用单一RGB值来着色模型.此方法无需使用UV坐标.通过直接对顶点应用颜色 ...

  2. Spring cloud公共模块

    1.0公共的模块是公共的工具包以及实体等 2.添加架包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...

  3. MongoDB_"Error parsing YAML config file: yaml-cpp: error at line 3, column 9: illegal map value"解决方法

    在启动配置文件的时候,系统报错:Error parsing YAML config file: yaml-cpp: error at line 3, column 9: illegal map val ...

  4. windows 命令行 for 用法

    for /r 目录名 %i in (匹配模式1,匹配模式2) do @echo %i for /r SATA %i in (*.txt) do @echo %i D:\REY\test>for ...

  5. 【airtest】iOS,Android 依托 jenkins 并行跑

    Airtest 只支持一台mac 连接一台iPhone,  以下方法是以“一台mac 连接一台iPhone”为基础,依托jenkins 统一管理多台iPhone. [mac] jenkins mast ...

  6. 网上的CSS例子编写都不太严谨,如*{ margin:0;padding:0;}

    margin:0;padding:0; 一般情况下不可以用 *{margin:0;padding:0;} 来适配. 保证自己的严谨代码编写风格.

  7. P3378 【模板】堆

    题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...

  8. [模板]Link-Cut-Tree

    LCT模板. Orz了一下大佬的板子 Orz UPD(10.19):好像理解LCT了... LCT相当与把一个树剖分,分成实边和虚边,对于每一个实链用一个splay维护一下它的深度,然后当你想进行操作 ...

  9. 记录python爬取猫眼票房排行榜(带stonefont字体网页),保存到text文件,csv文件和MongoDB数据库中

    猫眼票房排行榜页面显示如下: 注意右边的票房数据显示,爬下来的数据是这样显示的: 网页源代码中是这样显示的: 这是因为网页中使用了某种字体的缘故,分析源代码可知: 亲测可行: 代码中获取的是国内票房榜 ...

  10. 01.Python基础-5.函数

    1 函数的介绍 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 内置函数 自定义函数 2 函数的定义和调用 2.1 函数的定义和调用 定义 def 函数名([参数]): 代码块 [ ...