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 -->> ...
随机推荐
- android 五子棋开发
两天完成基本功能,再对其进行细节bug优化,本小白的思路. 思路: 1.用canvas绘制棋盘:得到手机的分辨率.棋盘大小为19*19.将手机宽屏分为21份,取中间19份为棋盘.上下空白位置为按钮功能 ...
- 如何使用Linux通用后门(转zafe)
特别提示:仅用于安全测试和教学,禁止非法用途. 标题党了,呵呵 其实就是个ssh后门,基本可以不用看内核版本,很简单,为照顾新手! ********************************** ...
- 函数中的$input
$input 在此属于一个特殊变量,一般在函数中用于接收输入 function FindWindowsFolder { $input | where-object {$_.Name -eq " ...
- [Arduino+Android] 自制土砲智能安全帽
专案动机(1/2) .现今社会中,各种交通运输载具方便了人类的生活,缩小了地域的差异性,当中车辆是人们日常生活中最频繁接触到的一部分. .车辆使人们的行动更加便利,也因此道路上行驶的车辆越来越多. . ...
- DotNet IOC Framework - Microsoft Unity介绍
一. 新建一个ASP.NET MVC4项目 二. 安装Microsoft Unity 1) 管理Nuget程序包 2)安装Unity3程序包 在你的App_Start文件夹里会多出来两个文件 三. 一 ...
- linux就是这个范儿之融于心而表于行(1)
原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处 .作者信息和本声明.否则将追究法律责 时间总是过得那么快,如流水一般哗啦啦的就淌走了一大堆!周遭事事沧桑变迁喧哗或耳语中流传的故事已渐模糊 ...
- c# 调用 c++写的DLL
http://www.cnblogs.com/MarsPanda/archive/2012/09/03/2668522.html 解决办法 安装 vcredist 运行库 或者用VC6.0编写DLL ...
- 关于开源的RTP——jrtplib的使用
session.BeginDataAccess(); if (session.GotoFirstSource()){ do{ RTPPacket *packet; while ((packet = s ...
- [Effective C++ --009]确定对象被使用前已先被初始化
在确保对象在使用前已先被初始化这一条款的编码实践中,作者为我们总结了三条经验,它们分别是: ------------------------------------------------------ ...
- 将项目添加到Finder侧边栏和工具栏
转: http://www.cnblogs.com/wormday/archive/2011/05/08/2039468.html 1.在侧边栏和工具栏右键,有相应的设置选项 2.可以将项目拖到侧边栏 ...