# UVA 1371 Period

dp[i][j]为B的前i个字符，且B的第i个字符已经匹配到A串的第j个字符时的最小修改次数。

dp[i][j] = min( dp[i][j] , dp[i][j-1] + 1 );  在i后面添加A串的第j个字符
dp[i][j] = min( dp[i][j] , dp[i-1][j] + 1 );  删除B串的第i个字符
dp[i][j] = min( dp[i][j] , dp[i-1][j-1] + (A[i]!=B[j]) );  修改B串第i个字符为A串第j个

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007

const int maxn = 5009;

char A[maxn],B[100];

int dp[maxn][55];
int La,Lb;

bool check( int k )
{
Clean(dp,0x3f);

dp[1][1] = A[1] != B[1];
rep(j,2,Lb) dp[1][j] = min( dp[1][j-1] + 1 , j-1 + (A[1]!=B[j]) );

rep(i,2,La)
{
if ( dp[i-1][Lb] <= k )
dp[i][1] = A[i] != B[1];
rep(j,1,Lb)
{
dp[i][j] = min( dp[i][j] , dp[i][j-1] + 1 );
dp[i][j] = min( dp[i][j] , dp[i-1][j] + 1 );
dp[i][j] = min( dp[i][j] , dp[i-1][j-1] + (A[i]!=B[j]) );
}
}
return dp[La][Lb] <= k;
}

int main()
{
int T;
cin>>T;
getchar();
while(T--)
{
scanf("%s%s",B+1,A+1);
La = strlen(A+1);
Lb = strlen(B+1);
int l = 0,r = 50;
int mid;
while( l < r )
{
mid = ( l + r )>> 1;
if ( check( mid ) )
r = mid;
else l = mid + 1;
}
cout<<l<<endl;
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：UVA 1371 Period 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)