hdu-1516 String Distance and Transform Process(编辑距离+路径)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1516

String Distance and Transform Process

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 227    Accepted Submission(s): 121
Special Judge


Problem Description
String Distance is a non-negative integer that measures the distance between two strings. Here we give the definition. A transform list is a list of string, where each string, except for the last one, can be changed to the string followed by adding a character, deleting a character or replacing a character. The length of a transform list is the count of strings minus 1 (that is the count of operations to transform these two strings). The distance between two strings is the length of a transform list from one string to the other with the minimal length. You are to write a program to calculate the distance between two strings and give the corresponding transform list.
 

Input
Input consists a sequence of string pairs, each string pair consists two lines, each string occupies one line. The length of each string will be no more than 80.
 

Output
For each string pair, you should give an integer to indicate the distance between them at the first line, and give a sequence of command to transform string 1 to string 2. Each command is a line lead by command count, then the command. A command must be

Insert pos,value
Delete pos
Replace pos,value

where pos is the position of the string and pos should be between 1 and the current length of the string (in Insert command, pos can be 1 greater than the length), and value is a character. Actually many command lists can satisfy the request, but only one of them is required.
 

Sample Input
   
   
   
   
abcac bcd aaa aabaaaa
 

Sample Output
   
   
   
   
3 1 Delete 1 2 Replace 3,d 3 Delete 4 4 1 Insert 1,a 2 Insert 2,a 3 Insert 3,b 4 Insert 7,a

题意:两个字符串,可以对第一个字符串进行三种操作:1.删除一个字符  2.插入一个字符  3.替换一个字符 使第一个字符串变成第二个字符串,求最少做几次操作

思路:用DP可以求得编辑距离,再从后面开始打印可以打印出路径。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 85
#define INF 1<<30
char a[N],b[N];
int dp[N][N];
int minn(int a,int b,int c)
{
    int m=a<b?a:b;
    return m<c?m:c;
}
int main()
{
    while(~scanf("%s %s",a,b))
    {
        int len=strlen(a);
        int len1=strlen(b);
        for(int i=0; i<=len1; i++)
            dp[0][i]=i;
        for(int i=0; i<=len; i++)
            dp[i][0]=i;
        for(int i=1; i<=len; i++)
        {
            for(int j=1; j<=len1; j++)
            {
                int val=(a[i-1]==b[j-1]?0:1);
                dp[i][j]=minn(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+val);
            }
        }
        printf("%d\n",dp[len][len1]);

        int is1=len,is2=len1;
        for(int i=1; i<=dp[len][len1];)
        {
            int flag;
            if(is1==0&&is2!=0) flag=2;
            else if(is1!=0&&is2==0) flag=1;
            else if(is1!=0&&is2!=0)
            {
                if(dp[is1-1][is2-1]==minn(dp[is1-1][is2],dp[is1][is2-1],dp[is1-1][is2-1]))
                    if(dp[is1-1][is2-1]==dp[is1][is2]) flag=0;
                    else flag=3;
                else if(dp[is1-1][is2]==minn(dp[is1-1][is2],dp[is1][is2-1],dp[is1-1][is2-1])) flag=1;
                else if(dp[is1][is2-1]==minn(dp[is1-1][is2],dp[is1][is2-1],dp[is1-1][is2-1])) flag=2;(注意这三个判断语句顺序,或者全部改为if。  有相同字母存在时优先选择相同字母的路径。)
            }   
            if(flag==0)
            {
                is1--;
                is2--;
            }
            else if(flag==1)
            {
                printf("%d Delete %d\n",i++,is1--);
            }
            else if(flag==2)
            {
                printf("%d Insert %d,%c\n",i++,is1+1,b[--is2]);
            }
            else if(flag==3)
            {
                printf("%d Replace %d,%c\n",i++,is1--,b[--is2]);
            }
        }
    }
    return 0;
}


你可能感兴趣的:(ACM,HDU)