OpenJudge/Poj 1159 Palindrome
1.链接地址:
http://bailian.openjudge.cn/practice/1159/
http://poj.org/problem?id=1159
2.题目:
Palindrome
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 49849 Accepted: 17153 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
Ab3bdSample Output
2Source
3.思路:
这题要知道其实最少增加的个数= 字符串总字数 - LCS(最长公共子序列)
所以就转化为求LCS
LCS为典型的dp算法之一,时间复杂度O(n^2),空间复杂度O(n)
!!!这题用string会超时,郁闷。开始对string没好感
4.代码:
#include <iostream>
#include <cstdio>
#include <cstring> #define max(a,b) ((a) > (b) ? (a) : (b)) using namespace std; int same(char ch1,char ch2)
{
if(ch1 == ch2) return ;
else return ;
} int LCS(char *str1,char *str2,int len1,int len2)
{
int i,j; //if(len1 < len2) {char *str3 = str1;str1 = str2;str2 = str3;} int **dp = new int*[];
for(i = ; i < ; ++i) dp[i] = new int[len2 + ];
memset(dp[],,sizeof(int) * (len2 + ));
dp[][] = ; for(i = ; i <= len1; ++i)
{
for(j = ; j <= len2; ++j)
{
dp[i % ][j] = max(dp[(i - ) % ][j],max(dp[i % ][j - ],dp[(i - ) % ][j - ] + same(str1[i - ],str2[j - ])));
//cout<<"dp[" << i << "][" << j << "]=" << dp[i % 2][j] << endl;
}
}
int max = dp[len1 % ][len2]; for(i = ; i < ; ++i) delete [] dp[i];
delete [] dp; return max;
} int main()
{
int n;
cin>>n; char *str1 = new char[n];
char *str2 = new char[n]; int i;
for(i = ; i < n; ++i)
{
cin>>str1[i];
str2[n - - i] = str1[i];
} int lcs_len = LCS(str1,str2,n,n); cout<<(n - lcs_len)<<endl; delete [] str1;
delete [] str2;
return ;
}
OpenJudge/Poj 1159 Palindrome的更多相关文章
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- 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 -->> ...
随机推荐
- Ognl表达式语言
l OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts2框架使用OGNL作为默认的表达式语言. ...
- EF中Database.SqlQuery
本文转载:http://www.cnblogs.com/daimage/archive/2012/07/04/2575844.html EF中Database.SqlQuery<TElement ...
- 【转】linux下cppunit的安装
以下内容来自:http://www.51testing.com/html/51/279751-170160.html 1. 安装 cppunit的下载地址为:http://sourceforge.ne ...
- Codeforces Round #325 (Div. 2) A. Alena's Schedule 水题
A. Alena's Schedule Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/pr ...
- android学习日记04--开发中的通用细节
1.android中的计量单位 px (pixels)(像素):是屏幕的物理像素点,与密度相关,密度大了,单位面积上的px会比较多.通常不推荐使用这个 pt(磅):1/72英寸,也较少用 in(英寸) ...
- yii 2.0 代码阅读 小记
1.\yii\base\object 设置了get/set属性...使用getName()获取属性名..构造函数中使用config初始化属性 2.\yii\base\Component 继承自Obje ...
- dd,实现系统备份
一.dd的使用 dd命令的解释dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾则乘以相应的数字:b=512:c=1:k=1024:w=2 参数: ...
- javascript之css常用属性
1. position : 属性值有absolute .fixed.relative absolute:生成绝对定位的元素,相对第一父元素进行定位: fixed : 生成绝对定位的元素,相对于浏览 ...
- Socket异步通信学习三
接下来是客户端部分,采用同步接收模式,在SocketClient项目中新建了一个SynServer类,用于存放socket服务器代码,和AsynServer类似,主要有4个方法: 有一个全局socke ...
- Http抓包工具Charlse使用教程
[原文]http://blog.csdn.net/wildfireli/article/details/19829479 Charles是目前最强大的http调试工具,在界面和功能上远强于Fiddle ...