标签:
题目大意:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; const LL INF = 0xfffffff; const LL maxn = 105; int dp[maxn][maxn], ans[maxn]; char a[maxn], b[maxn]; int DFS(int L,int R) { if(dp[L][R]) return dp[L][R]; if(L == R) return dp[L][R] = 1; if(L > R) return 0; dp[L][R] = DFS(L+1,R) + 1; for(int k=L+1; k<=R; k++) { if(b[L] == b[k]) dp[L][R] = min(dp[L][R], DFS(L+1,k) + DFS(k+1,R) ); } return dp[L][R]; } int main() { while(cin >> a >> b) { memset(dp, 0, sizeof(dp)); int n = strlen(a); for(int i=0; i<n; i++) DFS(0, i); for(int i=0; i <n; i++) { ans[i] = dp[0][i]; if(a[i] == b[i]) ans[i] =i?ans[i-1]:0; for(int j=0; j<i; j++) ans[i] = min(ans[i], ans[j]+dp[j+1][i]); } printf("%d\n", ans[n-1]); } return 0; }
=========================================================================
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; const LL INF = 0xfffffff; const LL maxn = 105; int dp[maxn][maxn], ans[maxn]; char a[maxn], b[maxn]; int main() { while(cin >> a >> b) { memset(dp, 0, sizeof(dp)); int n = strlen(a); for(int len=0; len<n; len++) { for(int i=0; i+len<n; i++) { int j = i + len; dp[i][j] = dp[i+1][j] + 1; for(int k=i+1; k<=j; k++) { if(b[i] == b[k]) { dp[i][j] = min(dp[i][j], dp[i+1][k] + dp[k+1][j]); } } } } for(int i=0; i <n; i++) { ans[i] = dp[0][i]; if(a[i] == b[i]) ans[i] =i?ans[i-1]:0; for(int j=0; j<i; j++) ans[i] = min(ans[i], ans[j]+dp[j+1][i]); } printf("%d\n", ans[n-1]); } return 0; }
HDU 2476 String painter(记忆化搜索, DP)
标签:
原文地址:http://www.cnblogs.com/chenchengxun/p/4836290.html