HDU 2895 编辑距离

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char str1[100005],str2[100005];
bool vis[100005];
int main(){

    while(scanf("%s",str1)!=EOF){
    getchar();
    memset(str2,0,sizeof(str2));
    memset(vis,false,sizeof(vis));
    scanf("%s",str2);
    getchar();
    int len1=strlen(str1);
    int len2=strlen(str2);
    int flag=0;
    int len;
    if(len1>len2){
        flag=1;
          len=len1-len2;
        }
    else if(len1==len2){
        flag=2;
          len=0;
        }
    else if(len1<len2){
        flag=3;
          len=len2-len1;
        }
    int End=0;
    int temp;
      for(int i=len2-1;i>=0;i--){
            temp=0;
          for(int j=0;j<len1;j++){
            if(str2[i]==str1[j]&&!vis[j]){
                End++;
                vis[j]=true;
                temp=1;
                break;
            }
          }
         if(temp==0)
            break;
      }

    if(flag==1){
        for(int i=0;i<len;i++){
            printf("d\n");
        }
        for(int i=0;i<len2;i++){
            printf("m %c\n",str2[i]);
        }

    }

    else  if(flag==2){
        for(int i=0;i<len2;i++)
            printf("m %c\n",str2[i]);

    }

    else if(flag==3){
        for(int i=0;i<len;i++){
            printf("a %c\n",str2[i]);
        }
        for(int i=len;i<len2;i++){
            printf("m %c\n",str2[i]);
        }

    }
    memset(str1,0,sizeof(str1));
    }
   return 0;
}

E - Edit distance

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 2895

Description

Given a string, an edit script is a set of instructions to turn it into another string. There are
four kinds of instructions in an edit script:

Add (‘a’): Output one character. This instruction does not consume any characters
from the source string.

Delete (‘d’): Delete one character. That is, consume one character from the source string and output nothing.

Modify (‘m’): Modify one character. That is, consume one character from the source string and output a character.

Copy (‘c’): Copy one character. That is, consume one character from the source string and output the same character.

Now, We define that A shortest edit script is an edit script that minimizes the total number of adds and deletes.

Given two strings, generate a shortest edit script that changes the first into the second.

Input

The input consists of two strings on separate lines. The strings contain only alphanumeric
characters. Each string has length between 1 and 10000, inclusive.

Output

The output is a shortest edit script. Each line is one instruction,
given by the one-letter code of the instruction (a, d, m, or c),
followed by a space, followed by the character written (or deleted if
the instruction is a deletion).

In case of a tie, you must generate shortest edit script, and must sort in order of a , d, m, c.

Therefore, there is only one answer.

Sample Input

abcde
xabzdey

Sample Output

a x
a a
m b
m z
m d
m e
m y

时间: 2024-11-08 23:47:30

HDU 2895 编辑距离的相关文章

HDU 2895 贪心 还是 大水题

DESCRIPTION:大意是给你两个字符串.编辑距离只有add和delete会产生.所以.编辑距离最短一定是两个字符串的长度差.然后...呵呵呵呵.... 猜题意就可以了...但是...我觉得这个题很不专业...题面上给的是删除时不输出字符...raner...还是要输出...还有就是...如果恰好当前对应的两个位置刚好相同..还是要modify...逗我玩呢.... 总而言之..已无力吐槽... 附代码: #include<stdio.h>#include<string.h>#

hdu 2895 01背包 Robberies

Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24462    Accepted Submission(s): 9037 Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that

hdu 4323 Magic Number (dp,编辑距离)

链接:hdu 4323 题意:给定n个串和m次询问,对于每次询问,给定一个字符串t,和最大操作次数a, 问在n个字符串中有多少个能在规定的次数之内变成字符串t. 说明:字符串的基本操作仅为:删除.插入和修改一个字符这三种操作 我们把进行了一次上述三种操作的任意一种操作称为进行了一步字符基本操作. 两个字符串的编辑距离:两个字符串a和b,通过上述的基本操作,把a变成b或b变成a, 需要的最少基本字符操作步数称为字符串a和字符串b的编辑距离 分析:分别求出n个字符串与字符串t之间的编辑距离,并判断是

HDU 4323 Magic Number(编辑距离DP)

http://acm.hdu.edu.cn/showproblem.php?pid=4323 题意: 给出n个串和m次询问,每个询问给出一个串和改变次数上限,在不超过这个上限的情况下,n个串中有多少个串可以转化为询问中给的串. 思路: 明显的编辑距离DP,直接暴力过了,网上有用bk树的,可惜我不会. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};