标签:ret algo 问题 pac txt algorithm 不难 print 直接
做法:
#include <cstdio>
#include <algorithm>
#include <cstring>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
const int N = 200 + 7;
const int inf = 0x3f3f3f3f;
typedef long long ll;
using namespace std;
char A[N],B[N],v[N];
int n,dp[N][N],f[N],cc,num[N];
void init_dp() {
rep(i,1,n)rep(j,i,n)dp[i][j]=j-i+1;
rep(len,1,n) {
rep(l,1,n-len+1) {
int r = l + len - 1;
dp[l][r] = dp[l+1][r] + 1;
rep(k,l+1,r)if(B[l]==B[k]){
dp[l][r] = min(dp[l][r],dp[l+1][k]+dp[k+1][r]);
}
}
}
}
int main() {
//freopen("in.txt","r",stdin);
while(~scanf(" %s %s",A+1,B+1)) {
n = strlen(A+1);
init_dp();
rep(i,1,n) f[i] = dp[1][i];
rep(i,1,n){
if(A[i]==B[i]) f[i] = f[i-1];
else {
rep(j,1,i-1) f[i] = min(f[i],f[j]+dp[j+1][i]);
}
}
printf("%d\n",f[n]);
}
return 0;
}
标签:ret algo 问题 pac txt algorithm 不难 print 直接
原文地址:https://www.cnblogs.com/RRRR-wys/p/9438638.html