Test instructions
Given two strings, a, A, a, B, and then each substring is edited into string A, the maximum number of edits in all substrings is the largest number of edits in the current state, requiring the minimum maximum number of edits.
Edit operations include modify, delete, and insert. (| a|<=5000,| B|<=50)
Analysis:
Because the length of a is large, it is not advisable to directly calculate the number of edits per interval corresponding to B. Because it is the minimization of the maximum, you can think of two answers and then judge.
When judging, use dp,dp[i][j] to indicate the maximum number of edits to the 1~i string match to J. When Dp[i][m]<=mid (mid is two points out of the current answer), you can set dp[i][0] to 0, indicating as the starting point (because at this point you can fragment after I).
A problem when playing code, when I dp[i][m]<=mid, I put dp[i][0] 0, but also the other values of dp[i] all into the INF, this is problematic. For example, X=ABCDABCABB,Y=ABC, when Mid=1, the first two characters AB's editing length is exactly one, at this point I directly put AB into a section, so that I put the third character C aside. Actually put ABC together for a better. The problem is that I put dp[i][0] at 0 and also put the other values of dp[i] into INF. This is equivalent to the default I divide the front of I into a paragraph, but in fact perhaps add a character editing distance still does not exceed mid, this may make the later answer better and I did not find the optimal solution, and then produced a judgment error. So just put dp[i][0] 0, this means that we can divide the characters in front of me into a paragraph, of course, you can add some characters and then into a section.
The code is as follows:
1#include <cstdio>2#include <algorithm>3#include <cstdlib>4#include <cstring>5#include <iostream>6 using namespacestd;7 #defineMAXN 50108 #defineMAXM 609 Ten CharA[MAXN],B[MAXM]; One intn,m; A - intF[MAXN][MAXM]; - the intMymin (intXintY) {returnX<y?x:y;} - - BOOLdpintx) - { +Memset (F, the,sizeof(f)); - for(intI=0; i<=m;i++) f[0][i]=i; + for(intI=1; i<=n;i++) A { at for(intj=1; j<=m;j++) - { - if(A[i]==b[j]) f[i][j]=mymin (f[i][j],f[i-1][j-1]); - ElseF[i][j]=mymin (f[i][j],f[i-1][j-1]+1); -F[i][j]=mymin (f[i][j],f[i-1][j]+1); -F[i][j]=mymin (f[i][j],f[i][j-1]+1); in } - if(f[i][m]<=x) to { + if(i==n)return 1; -f[i][0]=0; the } * if(i==n)return 0; $ }Panax Notoginseng } - the voidFfind () + { Ascanf"%s%s", B +1, A +1); theN=strlen (A +1); M=strlen (b +1); + intL=0, r=N; - while(l<R) $ { $ intMid= (l+r) >>1; - if(DP (mid)) r=mid; - ElseL=mid+1; the } -printf"%d\n", L);Wuyi } the - intMain () Wu { - intT; Aboutscanf"%d",&T); $ while(t--) - { - Ffind (); - } A return 0; +}uva1371
2016-03-06 16:47:51
Seems to have seen a lot of two points plus DP ~
"uva1371" Period (two minutes +DP)