您的位置:首页 > 其它

简单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的最小添加的字符串的数量;

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: