POJ 1159
来源:互联网 发布:皂基洗面奶知乎 编辑:程序博客网 时间:2024/06/05 13:22
原题网址:http://poj.org/problem?id=1159
题意:给一个字符串,求最少插入多少个字符可以使之变成回文串
题目分类:区间dp
状态转移方程:dp[i][j]=dp[i+1][j-1] (str[i]==str[j)
dp[i][j]=min(dp[i+1][j],dp[i][j-1]) (str[i]!=str[j])
题意:给一个字符串,求最少插入多少个字符可以使之变成回文串
题目分类:区间dp
状态转移方程:dp[i][j]=dp[i+1][j-1] (str[i]==str[j)
dp[i][j]=min(dp[i+1][j],dp[i][j-1]) (str[i]!=str[j])
注意:此题要用到滚动数组,直接开int二维数组会MLE(换成short就不会MLE,汗),在用滚动数组时要状态转移要从后往前推,不然之前的状态会被覆盖掉,
ac代码:
#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int M=5000+5;char str[M];short dp[2][M];int n;int main(){while(~scanf("%d",&n)){scanf("%s",str);memset(dp,0,sizeof(dp));for(int i=n-2;i>=0;i--){for(int r=1;r<=n;r++){int j=i+r;if(str[i]==str[j]){if(i+1>=j-1)dp[i%2][j]=0;elsedp[i%2][j]=dp[(i+1)%2][j-1];}else{if(i+1>=j)dp[i%2][j]=1;elsedp[i%2][j]=min(dp[i%2][j-1]+1,dp[(i+1)%2][j]+1);}}}printf("%d\n",dp[0][n-1]);}return 0;}
0 0
- POJ 1159
- poj 1159
- poj 1159
- POJ 1159
- poj 1159
- poj 1159
- poj 1159
- poj-1159
- POJ 1159
- POJ 1159
- POJ 1159
- POJ-1159
- POJ 1159
- poj 1159
- poj 1159 Palindrome LCS
- poj 1159 Palindrome LCS
- POJ 1159解题报告
- poj 1159 Palindrome
- scala学习十三 开发twitter客户端
- 100+V6(爱奇艺手机) root教程_方法
- 黑马程序员_动态代理
- Android:图片压缩类ThumbnailUtils
- 配置C程序的编译环境,及怎样演示Demo
- POJ 1159
- [Phonegap+Sencha Touch] 移动开发21 Sencha touch tapHold事件 触发时间太长的解决办法
- 百度面试题
- 2014/8/3 记
- PUE简介
- poj 1850(组合数学)
- [Phonegap+Sencha Touch] 移动开发22、安卓4.0.X的webview或自带浏览器中,去除输入框外面的蓝色边框
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:浅拷贝和深拷贝
- [Phonegap+Sencha Touch] 移动开发23 Android和IOS的webview点击穿透的缓解办法