hdu4271 Find Black Hand 2012 Changchun Online game e-Question shortest edit distance Find black handtime limit:5000/2000ms (java/other) Memory LIMIT:32768/32 768K (Java/other) total submission (s): Accepted submission (s): 1Problem Descriptioni as playing game with my Frien DS, although sometimes look pretty naive. Today I invent a new game called find Black Hand. The game is isn't about catching bad people but playing on a string.
Now I generate a string S and several short ones s[i], and I define three kinds of operations.
1. Delete:remove the ith character.
2. Insert:in any position, Insert a character if you like.
3. Change:change the ith character into another character if you like.
For each of the short string s[i], we define a function f (i). After several operations on S, we can find a substring of s which are the same to s[i]. and f (i) is the minimal number of operations to achieve. It looks so native that I think every one of the can solve F (I) perfectly. So I joins the string S from end to end, and F (I) changes nothing. The string "BB" is also a substring of string "Baaab".
The "Black Hand" is the short string s[i] whose f (i) is minimal. Now it's your time to find the Black hand.
Inputthere is multiple test cases. The first line contains a non-empty string S whose length are not more than 100,000. The next line contains a integer n (1 <= n <=) indicating the number of the short string. Each of the next N lines contains a short non-empty string whose length are not more than 10. All strings of the input would not has blank and all characters is lower case.
Outputfor each test case, output a string first indicating the ' Black Hand ', and then output an integer indicating the min iMAL number of the operation. If there is more than one ' Black Hand ', please output the smallest one in lexicographical order.
Sample Inputaaabbbb2alice Bob
Sample Outputbob 1
Source2012 ACM/ICPC Asia Regional Changchun Online
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespacestd;Const intmaxn=200100;Const intInf= (1<< in);CharS[MAXN];Chart[ -][ -];intN;intdp[ -][MAXN];intDP (Char*t,Char*s,intMintN) { intres=INF; for(intI=0; i<=m;i++){ for(intj=0; j<=n;j++) dp[i][j]=0; } for(intI=1; i<=m;i++) {dp[i][0]=i; for(intj=1; j<=n;j++) {Dp[i][j]=min (min (dp[i-1][j]+1, dp[i][j-1]+1), dp[i-1][j-1]+ (t[i-1]!=s[j-1])); if(i==m) res=min (res,dp[i][j]); } } returnRes;}intMain () {Freopen ("In.txt","R", stdin); while(SCANF ("%s", s)! =EOF) {scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%s", T[i]); intlen=strlen (s); intUp=min ( -, Len); for(intI=0; i<up;i++) s[i+len]=R[i]; S[up+len]=' /'; intans=inf,p=1; for(intI=1; i<=n;i++){ intm=strlen (T[i]); inttmp=INF; if(m<=Len) {tmp=min (TMP,DP (t[i],s,m,len+Up )); } Else{ Charnow[ -]; for(intj=0; j+len<len+up;j++) {strncpy (now,s+J,len); TMP=min (TMP,DP (T[i],now,m,len)); } } if(Tmp<ans) ans=tmp,p=i; Else if(TMP==ANS&&STRCMP (T[i],t[p]) <0) p=i; } printf ("%s%d\n", T[p],ans); } return 0;}
View Code
hdu4271 Find Black Hand 2012 Changchun Online game e-question shortest editing distance