HDU 2895 编辑距离

本文介绍了一种解决字符串转换问题的算法——编辑距离算法,并详细解释了如何通过添加、删除、修改和复制操作来将一个字符串转换为另一个字符串。文中提供了一个具体的C++实现示例,演示了如何生成最短编辑脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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

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
 

转载于:https://www.cnblogs.com/13224ACMer/p/4671300.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值