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 -->> ...
随机推荐
- HDU 2112 HDU Today -- from lanshui_Yang
此题主要是要用到字符串向整数的映射 , 很自然的想到了 STL 中的map ,哎,贡献无数次WA,最后才发现每次运行时 map 忘了清空 !!!!本题,用dijkstra 和 spfa 均可 ,但是要 ...
- btrace拓展工具-java应用性能诊断优化利器
Btrace是一个实时监控工具,可以无需修改应用代码(事实上它修改了字节码),来达到不可告人的秘密!这是性能调优和诊断的利器! 它可以获取应用程序代码的执行时间,他可以让你无需修改代码,帮你做时间的打 ...
- 【Android】 图片编辑:创建圆角图片
创建圆角图片的方式大同小异,最简单的就是 9.png 美工做出来的就是.这种最省事直接设置就可以. 另外一种就是通过裁剪 这里的剪裁指的是依据原图我们自己生成一张新的bitmap,这个时候指定图片的目 ...
- 【转】SQL语句中的正则表达示
正则表达式作用是匹配方本,将一个模式(正则表达式)与一个文本串进行比较. MySQL用WHERE子句对正则表达式提供了初步的支持,允许你指定用正则表达式过滤SELECT检索出的数据. MySQL仅支持 ...
- Java对MySQL数据库进行连接、查询和修改【转载】
一般过程: (1) 调用Class.forName()方法加载驱动程序. (2) 调用DriverManager对象的getConnection()方法,获得一个Connection对象. (3) 创 ...
- Unity3d 网络编程(一)(Unity3d内建网络Network介绍)
首先个人说说题外话,Unity3d使用的网络库核心是用C++实现的一款商业网络游戏引擎库. RakNet.所以对于移动设备来说,用Unity3d来写server是全然能够的,而且内建网络库的各项功能封 ...
- android学习日记20--连接组件之Intent和IntentFilter
上次刚了解完Android的四大组件,现在学习组件间通信的Intent和IntentFilter 一.Intent 1.简述 Intent(意图)在应用程序运行时连接两个不同组件,是一种运行时的绑定机 ...
- 自学JavaScript笔记
最近看了一段时间的<JavaScipt高级编程设计>由于记性不是很好,经常性的看了又忘记:想一些文字整理在自己的博客上,方便没事都可以拿出来看一下: 第一章 JavaScript概述 ...
- jsp HTTP Status 405 - HTTP method GET is not supported by this URL
package myservlet.control; import java.io.IOException; import java.io.PrintWriter; import javax.serv ...
- UNIX V6内核源码剖析——unix v6 全貌
1. UNIX V6 运行硬件环境——PDP-11/40 PDP-11/40指令和数据都是以16比特为单位.对它而言,一个字的宽度为16比特. PDP-11/40以及周边设备的寄存器被映射到内存最高位 ...