简单DP(51nod 1092)
2016-04-04 21:02
363 查看
题目:回文字符串
思路:找准状态以及决策,就可以了;
形如:E[i,j]=opt{D[i-1,j]+xi,D[i,j-1]+yj,D[i-1][j-1]+zij} (最长公共子序列)
变形即可;
dp[i][j]是第i位置开始长度为j的最小添加的字符串的数量;
思路:找准状态以及决策,就可以了;
形如:E[i,j]=opt{D[i-1,j]+xi,D[i,j-1]+yj,D[i-1][j-1]+zij} (最长公共子序列)
变形即可;
dp[i][j]是第i位置开始长度为j的最小添加的字符串的数量;
#include <iostream> #include <algorithm> #include <stdlib.h> #include <time.h> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <queue> #include <stack> #include <set> #define c_false ios_base::sync_with_stdio(false); cin.tie(0) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3f #define zero_(x,y) memset(x , y , sizeof(x)) #define zero(x) memset(x , 0 , sizeof(x)) #define MAX(x) memset(x , 0x3f ,sizeof(x)) #define swa(x,y) {LL s;s=x;x=y;y=s;} using namespace std ; #define N 1005 const double PI = acos(-1.0); typedef long long LL ; int dp ; char a ; int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); //ios_base::sync_with_stdio(false); cin.tie(0); scanf("%s", a); int n = strlen(a); memset(dp,0,sizeof(dp)); for(int j = 1; j <= n; j++){ for(int i = 0; j+i-1 < n; i++){ dp[i][j] = min(dp[i][j-1], dp[i+1][j-1]) +1; if(a[i] == a[i+j-1]) dp[i][j] = min(dp[i+1][j-2], dp[i][j]); } } cout<<dp[0] <<endl; return 0; }
相关文章推荐
- 【Unity&C#】写了一个事件分发器
- Android 拖放(OnDragListener)
- Chapter 15:JSON Assignment
- jQuery
- LeetCode笔记:242. Valid Anagram
- java动态获取jvm参数
- 团队伊始——DreamCatcher
- 【opencv+python教程笔记】1. 颜色空间,二值化
- cf18B Platforms (贪心策略_好题)
- 【ZOJ 1909】 square
- BlueStacks如何调整屏幕分辨率和屏幕宽度
- android coredump 调试
- eclipse+adt+sdk+genymotion搭建android开发环境
- paper 51:图像复原
- Shell中的字符串操作
- 第73课内幕资料详细版 Spark SQL Thrift Server 实战 每天晚上20:00YY频道现场授课频道68917580
- Coin Change (II)
- HDOJ 2083 简易版之最短距离
- proj4.8编译
- 大型网站性能优化技巧