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 -->> ...
随机推荐
- eclipse代码左虚线对齐设置
前言 : 前阵子看到同事的eclipse左边有虚线对齐.有点好奇~刚开始以为是装了神马插件, 于是百度了下, 貌似没有找着. 一怒之下,逗比了半个小时,终于探索出来了~~ 设置如下 : 点击confi ...
- ABAP DEBUG
[Function] Command=/H Type=SystemCommand 将上面的文件推动到SAP 窗口 可以启动调试 ------------------------------------ ...
- 统计php源码行
嘿嘿,最近在提交文件,需要知道代码行数,简单记录下,由几种不同的方法进行: 1.直接在 linux 上运行下面语句即可,秒杀: find . -name "*.php" -exec ...
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
转载:http://blog.csdn.net/xiaanming/article/details/12684155 转载请注明出处:http://blog.csdn.net/xiaanming/ar ...
- 今天写一些 有关iOS 多图片组合 成一张图片的问题。保持原像素不变
1.要求:服务器给一张图片模板,要在模版上镂空,然后添加一些别的图片,然后组合成一张图,这个模版的像素 不是固定的,有可能比 当前手机屏幕大.所以,在组合截图的时候,有一定的要求. 贴代码: /** ...
- WINHTTP的API接口说明。
BOOL WINAPI WinHttpAddRequestHeaders( _In_ HINTERNET hRequest, _In_ LPCWSTR pwszHeaders, _In ...
- iOS开发笔记系列-基础1(数据类型与表达式)
学习iOS开发快两年了,去年完成MagViewer之后就因为公司的其他业务繁重,除了维护这个应用之外,只是断断续续地自己做一些实验开发,没有再发布新的应用,这里想整理一下学习过程中的笔记,以便加深印象 ...
- PrintJ的设计模式之旅——1.模式之父
好奇设计模式的源头,做了一番搜索和调查,于是便开启了这个系列“PrintJ的设计模式之旅”. 1.模式之父 GOF(Gang of Four) Erich Gamma.Richard Helm.Ral ...
- 应用之星推出“图文app”制作工具,并附上教程
应用之星已推出的"图文"app制作工具,是高速制作图文电子书,图文杂志等一切有关图文资料的app生成工具,以下跟大家介绍"图文"制作教程,简单快捷,大致分三大步 ...
- android学习日记27--Dialog使用及其设计模式
1.Dialog概述 对话框一般是一个出现在当前Activity之上的一个小窗口,处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的小功 ...