标签:dp
J - 买票回家啦
Description
Input
Output
Sample Input
3 abab aeqa baab
Sample Output
1 1 0
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <climits> #include <cstring> #include <cmath> #include <map> #include <set> #define INF 100000000 using namespace std; int n; char a[1005]; int dp[1005][1005]; int fun(int l,int r){ if(l > r) return 0; if(dp[l][r]) return dp[l][r]; //cout << 'B' ; if(l == r){ return dp[l][r] = 1; } else{ if(a[l] == a[r]){ return dp[l][r] = (2 + fun(l+1,r-1)); } else{ return dp[l][r] = max(fun(l+1,r),fun(l,r-1)); } } } int main(){ int t; //freopen("in.txt","r",stdin); cin >>t; while(t--){ scanf("%s",a); memset(dp,0,sizeof(dp)); int len = strlen(a); printf("%d\n",len - fun(0,len-1)); } return 0; }
标签:dp
原文地址:http://blog.csdn.net/qq_24667639/article/details/45420991