###### 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(二分+dp)

2014-05-04 21:46:57

#### Period UVA - 1371

2017-10-20 11:15:16

#### UVA 1371 - Period（DP）

2014-05-03 23:54:35

#### UVA - 1371 Period 二分+dp

2017-03-26 21:09:46

#### 1371 - Period

2015-08-07 22:49:57

#### UVALive 3026 Period（KMP裸）

2015-09-07 22:56:58

#### UVa 1328 Period

2016-05-09 23:47:43

#### UVA 1328 Period(KMP:最短循环节)

2014-04-04 10:54:28

#### Timus 1371. Cargo Agency

2014-04-22 21:23:37

#### Norton PartitionMagic V8.05 Build 1371 简装汉化版.exe

2013年08月14日 4.29MB 下载

UVA 1371 Period