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 -->> ...
随机推荐
- iOS 开发笔记
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用) 2,NSDate使用 3,UTTabviewCell 未 ...
- 一些在IOS中关于JS、H5开发的网站
1.JSPatch 2.
- android 之 animations 动画
android 提供的了两种机制你可以用来创建简单的动画:tweedned animation(渐变动画) 和 frame-by-frame animation(逐帧动画)(有道翻译的,汗!!!) . ...
- 【转载】小米2进入recovery的方法
用过M1的朋友都多多少少的了解到~进入recovery是关机下同时 按 音量(+)+电源键. 其实M2也一样,但是我觉得是有点区别的. 在M1的时候,只要同时长按这两个键就可以的了. 但是M2呢?我发 ...
- IDO分享 | 如何在centos下安装OpenCMS
本次的opencms环境是在两台机器上搭建的. 一台服务器安装mySQL, 一台服务器安装jdk.tomcat.opencms.也可以将jdk.mySQL.tomcat.opencms安装在同一个机器 ...
- 【UXPA工作坊小记】郎学明:做更“有用”的用户研究
xueminglang@google.com 本来做了一些笔记,但郎老师后来发了相关教材.内容比现场PPT详细的多.由于,本人在网上也没有搜索到相关文章,还是决定做一回码字工,稍作精简后分享给大家. ...
- JavaScript闭包的底层运行机制
转自:http://blog.leapoahead.com/2015/09/15/js-closure/ 我研究JavaScript闭包(closure)已经有一段时间了.我之前只是学会了如何使用它们 ...
- MySQL 如何修改字符集 utf8 改为 utf8mb4
在实行sql server 向 mysql 迁移数据时,报错: Incorrect string value: '\xF0\x9F\x98\x8A' 原因是mysql 采用的是 utf8 的字符集,而 ...
- oracle的增删改查语句
创建一个表: cteate table 表名(列1 类型, 列2 类型);查看表结构 desc表名添加一个字段 alter table 表名 add(列类型);修改字段类型 alter table 表 ...
- c++基础回顾
#include <iostream> #include <vector> #include <string> int main(int argc, const c ...