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]=‘\0‘;
        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\n",t[p],ans);
    }
    return 0;
}

时间: 2024-11-02 17:37:03

hdu4271 Find Black Hand 2012长春网络赛E题 最短编辑距离的相关文章

hdu 5441 (2015长春网络赛E题 带权并查集 )

n个结点,m条边,权值是 从u到v所花的时间 ,每次询问会给一个时间,权值比 询问值小的边就可以走 从u到v 和从v到u算不同的两次 输出有多少种不同的走法(大概是这个意思吧)先把边的权值 从小到大排序 询问值也按从小到大排序num记录集合里元素的个数每合并两个集合 ans增加 2*num[u]*num[v] Sample Input15 5 3 //n w q2 3 63341 5 157243 5 57054 3 123821 3 2172660001000013000 Sample Out

hdu 5446(2015长春网络赛J题 Lucas定理+中国剩余定理)

题意:M=p1*p2*...pk:求C(n,m)%M,pi小于10^5,n,m,M都是小于10^18. pi为质数 M不一定是质数 所以只能用Lucas定理求k次 C(n,m)%Pi最后会得到一个同余方程组x≡B[0](mod p[0])x≡B[1](mod p[1])x≡B[2](mod p[2])......解这个同余方程组 用中国剩余定理 Sample Input19 5 23 5 Sample Output6 1 # include <iostream> 2 # include <

hdu 5442 (ACM-ICPC2015长春网络赛F题)

题意: 分析: 明天再写…… #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define d(x) const int MAX_N = (int)(4e4) + 100; //call init_RMQ(f[], n) first. //then call query(a, b) to quest the RMQ of [a, b]. int power[3

2015北京网络赛A题The Cats&#39; Feeding Spots

题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<memory.h> 6 using namespace std; 7 const i

西电校赛网络赛J题 lucas定理计算组合数

西电校赛网络赛J题  lucas定理计算组合数 问题 J: 找规律II 时间限制: 1 Sec  内存限制: 128 MB 提交: 96  解决: 16 [提交][状态][讨论版] 题目描述 现有数阵如下: 1    2  3   4     5    6 1   3   6  10  15 1   4  10   20 1   5   15 1    6 1 求这个数阵的第n行m列是多少(行列标号从1开始) 结果对10007取模 输入 多组数据,每组数据一行,包含两个整数n,m(1<=n<=

HDU 4815 2013长春现场赛C题

C - Little Tiger vs. Deep Monkey Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4815 Description A crowd of little animals is visiting a mysterious laboratory ? The Deep Lab of SYSU. "Are y

浙江2012年省赛J题 Modular Inverse

Modular Inverse Time Limit: 2000MS   Memory Limit: 65535KB   64bit IO Format: Submit Status Description The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x (mod m). This is equivalent to ax≡1 (mod m). I

HDU 5024 Wang Xifeng&#39;s Little Plot(广州网络赛C题)

HDU 5024 Wang Xifeng's Little Plot 题目链接 思路:先利用记忆化搜索预处理出每个结点对应8个方向最远能走多远,然后枚举拐点记录最大值即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int d[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {1,

ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn&#39;t want to study

C*M....死于update的一个long long写成int了 心累 不想写过程了 ******** 树状数组,一个平的一个斜着的,怎么斜都行 题库链接:https://nanti.jisuanke.com/t/31460 #include <iostream> #include <cstring> #define ll long long #define lowbit(x) (x & -x) using namespace std; const int maxn =