poj 1159 Palindrome
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 59094 | Accepted: 20528 |
Description
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
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
line contains one integer, which is the desired minimal number.
Sample Input
5
Ab3bd
Sample Output
2
Source
给一串字符,问最少加多少个字符可以使它变成回文字符串。
(回文字符串=从左往右看 和 从右往左看 串相同)
/* f[i][j]表示从字符标号i-j 变成回文串需要的最少步数
不难得出
若s[j]==s[i+j] : f[j][i+j]=f[j+1][i+j-1],
否则 : f[j][i+j]=min(f[j+1][i+j],f[j][i+j-1])+1
*/
#include<cstdio>
#include<iostream>
using namespace std;
#define N 5010
char s[N];int n;
short int f[N][N];
int main(){
scanf("%d%s",&n,s);
for(int i=;i<=n;i++){
f[i][i]=;f[i][i+]=s[i]==s[i+]?:;//初始化
}
for(int i=;i<=n;i++){//循环两个字母的间隔
for(int j=;i+j<n;j++){//循环开端字母
if(s[j]==s[i+j]) f[j][i+j]=f[j+][i+j-];
else f[j][i+j]=min(f[j+][i+j],f[j][i+j-])+;
}
}
printf("%d\n",f[][n-]);
return ;
}
poj 1159 Palindrome的更多相关文章
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- OpenJudge/Poj 1159 Palindrome
1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome T ...
- poj 1159 Palindrome - 动态规划
A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...
- poj 1159 Palindrome(dp)
题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...
- POJ 1159 Palindrome(LCS)
题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...
- poj 1159 Palindrome(区间dp)
题目链接:http://poj.org/problem?id=1159 思路分析:对该问题的最优子结构与最长回文子序列相同.根据最长回文子序列的状态方程稍加改变就可以得到该问题动态方程. 假设字符串为 ...
- POJ 1159 Palindrome(最长公共子序列)
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...
- POJ 1159 Palindrome(最长公共子序列)
http://poj.org/problem?id=1159 题意: 给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串. 思路: 计算出最长公共子序列,再用原长-LCS=所要添加的字符数 ...
- poj 1159 Palindrome 【LCS】
任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <io ...
- poj - 1159 - Palindrome(滚动数组dp)
题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...
随机推荐
- AT NEW F、AT END OF F注意事项
1.F只能是内表的第一个字段 2.AT NEW F.AT END OF F使用F之后内表内容会变为* 解决出现*的办法: FIELD-SYMBOLS:<ITAB> LIKE ITAB L ...
- 使用Reflexil修改类库
因为公司一个项目需要修改PPS的颜色,于是反编译了PPS的代码发现,作者很不厚道的把所有着色都HardCode在代码里 private static void InsertDefaultSeriesC ...
- Level 4 A10: 飞张?
看来庄家的红桃2个输张没法解决,只能寄希望于飞K了. 但如果将牌2-2分布,还有更稳的打法.在下面这种东家3张黑桃的情况时,庄家只需垫到红桃2就行了. 如果东家有4张黑桃,那就只有飞红桃K这一条路了.
- ubuntu解决libstdc++.so.6: cannot open shared object file: No such file or directory:问题
解决libstdc++.so.6: cannot open shared object file: No such file or directory:原因在于,在13.10 版本中,ia32_lib ...
- Android SDK Manager无法下载的问题
Android SDK Manager 你无法更新了. 现在这里有一个解决方案,如下. 1.启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」 ...
- 浅谈Hex编码算法
一.什么是Hex 将每一个字节表示的十六进制表示的内容,用字符串来显示. 二.作用 将不可见的,复杂的字节数组数据,转换为可显示的字符串数据 类似于Base64编码算法 区别:Base64将三个字节转 ...
- iOS第三方类库JSPatch(热更新)
---------------------------------------------------------------------------------------------------- ...
- 项目管理工具之Git使用说明
1.下载Git客户端工具 http://msysgit.github.com/ 2.安装msysgit 下一步 同意GNU协议 选择安装位置 选择TrueType Front,下一步 不创建启动文件 ...
- ajax跨域 自定义header问题总结
遇到的问题:已经在ajax里面添加自定义参数,但是没有生效 beforeSend : function(request) { request.setRequestHeader("region ...
- 动态规划--模板--hdu 1059 Dividing
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...