Palindrome(POJ 1159 DP)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 58168 | Accepted: 20180 |
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
Output
Sample Input
5
Ab3bd
Sample Output
2
最少插入多少个字符使得原来的字符串变成回文串DP[i][j]表示长度为i的第j个字符开头的字串需要插入的个数
状态转移方程:(字符串下表以1开始)
dp[i][j]=dp[i-2][j+1] 如果s[i+j-1]==s[j]
dp[i][j]=min(dp[i-1][j-1],dp[i-1][j+1])+1 如果不等
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define Max 5001
short int dp[Max][Max];
char s[Max];
int main()
{
int len;
int i,j;
freopen("in.txt","r",stdin);
scanf("%d",&len);
scanf("%s",s);
memset(dp,,sizeof(dp));
for(i=;i<=len;i++)
{
for(j=;j<=len-i;j++)
{ if(s[i+j-]==s[j])
dp[i][j]=dp[i-][j+];
else
dp[i][j]=min(dp[i-][j],dp[i-][j+])+;
}
}
printf("%d\n",dp[len][]);
return ;
}
Palindrome(POJ 1159 DP)的更多相关文章
- poj - 1159 - Palindrome(滚动数组dp)
题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...
- Sumsets(POJ 2229 DP)
Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 15293 Accepted: 6073 Descrip ...
- poj 3311(状态压缩DP)
poj 3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...
- poj 1185(状态压缩DP)
poj 1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...
- poj 3254(状态压缩DP)
poj 3254(状态压缩DP) 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相 ...
- 【POJ 3140】 Contestants Division(树型dp)
id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS Memory Limit: 65536K Tot ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- dp乱写1:状态压缩dp(状压dp)炮兵阵地
https://www.luogu.org/problem/show?pid=2704 题意: 炮兵在地图上的摆放位子只能在平地('P') 炮兵可以攻击上下左右各两格的格子: 而高原('H')上炮兵能 ...
随机推荐
- 集合:在我的世界里,你就是唯一 - 零基础入门学习Python027
集合:在我的世界里,你就是唯一 让编程改变世界 Change the world by program 好,上课之前小甲鱼考考大家还记不记得上节课的内容?上节课我们讲Python中的"字典& ...
- Asp.Net MVC3.0 Partial RenderPartial Action RenderAction 区别和用法
本人写的博文不多,专业知识不强,以下纯属于个人笔记.如有不对,还请各路大拿,拍砖指导,谢谢! 区别: 1.Partial 与 RenderPartial 两个方法性质基本一样,只是把一个静态用户控件给 ...
- 初识cross apply & outer apply
1. 2. 3.参考地址: http://blog.csdn.net/htl258/article/details/4537421
- oracle ed命令
今天在弄oracle sqlplus的时候需要写一个存储过程,用ed进入之后,退不出来了. 从网上找到下面的方法处理: 如何退出sqlplus ed操作 今天进入sqlplus ,执行完语句后 ...
- GridView 设置背景透明以及Item的点击动画
//将点击时的背景色设置为透明 gridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); 此时点击GridView的每个Item就不会出现 ...
- Android使用自带JSONObject解析JSON数据
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android ...
- 【转】将 Linux 应用程序移植到 64 位系统上
原文网址:http://www.ibm.com/developerworks/cn/linux/l-port64.html 随着 64 位体系结构的普及,针对 64 位系统准备好您的 Linux® 软 ...
- windows phone之获取当前连接WIFI的SSID
public string GetSSIDName() { foreach (var network in new NetworkInterfaceList()) { if ( (network.In ...
- Javascript或jQuery方法产生任意随机整数
方法1:javascritp方法 1 2 3 4 5 6 //随机数 function diu_Randomize(b,e){ if(!b && b!=0 || ! ...
- UIScrollView入门与框架设计
一.概述 1.UIScrollView的contentSize, contentOffSet, contentInsets的作用和使用. 2.UIScrollView的一整个滚动过程的生命周期(开始滚 ...