HDU 4271 Find Black Hand (DP)

Find Black Hand

Time Limit : 5000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 19   Accepted Submission(s) : 4
Problem Description
I like playing game with my friends, although sometimes look pretty naive. Today I invent a new game called find black hand. The game is not about catching bad people but playing on a string.<br>Now I generate a string S and several short ones s[i], and I define three kinds of operations.<br>1. Delete: remove the ith character.<br>2. Insert: in any position, insert a character if you like.<br>3. Change: change the ith character into another character if you like.<br>For each short string s[i], we define a function f(i). After several operations on S, we can find a substring of S which is 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 you can solve f(i) perfectly. So I join the string S from end to end, and f(i) changes nothing. So the string &quot;bb&quot; is also a substring of string &quot;baaab&quot;.<br>The &quot;black hand&quot; is the short string s[i] whose f(i) is minimal. Now it's your time to find the black hand. <br>
 

Input
There are multiple test cases.
The first line contains a non-empty string S whose length is not more than 100,000.
The next line contains an integer N (1 <= N <= 10) indicating the number of the short string.
Each of the next N lines contains a short non-empty string whose length is not more than 10.
All strings in the input would not have blank and all characters are lower case.
 

Output
For each test case, output a string first indicating the "black hand", and then output an integer indicating the minimal number of the operation. If there are more than one "black hand", please output the smallest one in lexicographical order.
 

Sample Input
aaabbbb
2
alice
bob
 

Sample Output
bob 1
 

Source
2012 ACM/ICPC Asia Regional Changchun Online

 

转自:http://m.blog.csdn.net/blog/yhc13429826359/7971897 

题意:给定串长<=100000的母串和n(n<=10)且长度<=10的小串,问哪一个小串在母串中通过增加删除修改一个字母的操作数(即编辑距离)最小变成
         母串中的substring,求最小的编辑距离,和对应字典序最小的字串。

题解:比赛的时候看了一眼,substring看着有点晃,就搞别的了,还是自己太弱最后没有时间仔细想想这个题目。

           对于两个串的编辑距离可以用O(n*m)的复杂度来搞,对于字串只需要做一点修改即可,具体见代码注释。
         对于长度为len的串,要想成为母串的substring最大的编辑距离不超过len。

         对于题目描述的问题,因为环的存在考虑把原串的前10位直接复制到原串后面,但是需要注意的是如果母串abcd,子串abcda,最少需要操作次数是1,
         但是将abcd复制后变成abcdabcd,abcda成了子串,答案变成了0,所以如果当前串长l*2 >= 母串长,需要循环原串起始位置暴力即可。

 代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
#define INF 10000000
char goal[200005],sm[20][20],huan[200005];
int dp[200005][20];
int cal(char *s1,int len1,char *s2,int len2)
{
    for(int i=0; i<=len1; i++)//别用memset快些
        for(int j=0; j<=len2; j++)
            dp[i][j] = 0;
    for(int j=0; j<=len2; j++)
        dp[0][j] = j;

    for(int i=1; i<=len1; i++)//从1开始!
        for(int j=1; j<=len2; j++)
        {
            int cost = (s1[i-1] == s2[j-1])? 0 : 1;//判断 i-1 和 j-1 别写成i,j......
            dp[i][j] = min(dp[i-1][j-1]+cost,min(dp[i-1][j]+1,dp[i][j-1]+1));
        }
    int res = INF;
    for(int i=0; i<=len1; i++)
        res = min(res,dp[i][len2]);
    return res;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%s",goal)!=EOF)
    {
        int n;
        scanf("%d",&n);
        int len1 = strlen(goal);
        
        for(int i=len1; i<len1*2; i++)//先复制一遍数组
            goal[i] = goal[i-len1];
        int ans = INF,xu;
        for(int i=0; i<n; i++)
        {
            scanf("%s",sm[i]);
            int len2 = strlen(sm[i]);
            if(len2 * 2 > len1)
            {
                for(int s=0; s<len1; s++)
                {
                    for(int j=0; j<len1; j++)
                    {
                        huan[j] = goal[(s+j) % len1];
                    }
                    huan[len1] = '\0';
                    int tmp = cal(huan,len1,sm[i],len2);
                    if(tmp < ans || tmp == ans && strcmp(sm[xu],sm[i])>0)
                        ans = tmp , xu = i;
                }
            }
            else
            {
                int tmp = cal(goal,len1+len2,sm[i],len2);
                if(tmp < ans || tmp == ans && strcmp(sm[xu],sm[i])>0)
                    ans = tmp , xu = i;
            }
        }
        printf("%s %d\n",sm[xu],ans);
    }
    return 0;
}
View Code
posted @ 2015-04-08 20:21  Doli  阅读(149)  评论(0编辑  收藏  举报