zoukankan      html  css  js  c++  java
  • hdu4271 Find Black Hand 2012长春网络赛E题 最短编辑距离

    hdu4271 Find Black Hand  2012长春网络赛E题  最短编辑距离

    Find Black Hand

    Time Limit : 5000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 19   Accepted Submission(s) : 1
    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.
    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 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 "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. 
     
    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
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    
    using namespace std;
    
    const int maxn=200100;
    const int INF=(1<<29);
    
    char s[maxn];
    char t[30][30];
    int n;
    int dp[30][maxn];
    
    int DP(char *t,char *s,int m,int n)
    {
        int res=INF;
        for(int i=0;i<=m;i++){
            for(int j=0;j<=n;j++) dp[i][j]=0;
        }
        for(int i=1;i<=m;i++){
            dp[i][0]=i;
            for(int j=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]);
            }
        }
        return res;
    }
    
    int main()
    {
        freopen("in.txt","r",stdin);
        while(scanf("%s",s)!=EOF){
            scanf("%d",&n);
            for(int i=1;i<=n;i++) scanf("%s",t[i]);
            int len=strlen(s);
            int up=min(20,len);
            for(int i=0;i<up;i++) s[i+len]=s[i];
            s[up+len]='';
            int ans=INF,p=1;
            for(int i=1;i<=n;i++){
                int m=strlen(t[i]);
                int tmp=INF;
                if(m<=len){
                    tmp=min(tmp,DP(t[i],s,m,len+up));
                }
                else{
                    char now[30];
                    for(int j=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
    ",t[p],ans);
        }
        return 0;
    }
    View Code
     
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    三、单例模式
    二、工厂模式
    一、设计模式六大原则
    十二、Spring之IOC容器初始化
    PythonWeb开发教程(二),搭建第一个django项目
    PythonWeb开发教程(一),开发之前需要准备什么
    基于python的性能测试工具–locust
    在成为测试大牛的路上,我推荐BestTest
    移动端自动化测试(一)appium环境搭建
    常用工具篇(二)死链接扫描工具–Xenu
  • 原文地址:https://www.cnblogs.com/--560/p/4781792.html
Copyright © 2011-2022 走看看