标签:
题目:回文字符串
思路:找准状态以及决策,就可以了;
形如: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[N][N]; char a[N]; 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][n]<<endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/yoyo-sincerely/p/5352911.html