hdu1516-String Distance and Transform Process(编辑距离)

 

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

 while(a>=1 || b>=1)
    {
        if(s[a-1]==s1[b-1]) tmp=0;
        else tmp=1;
        if(dp[a][b]==dp[a-1][b-1]+tmp && a>=1 && b>=1)
        {
            if(tmp)
            {
                printf("%d Replace %d,%c\n",++step,a,s1[b-1]);
            }
            a--,b--;
        }
        else if(dp[a][b]==dp[a-1][b]+1 && a>=1)
        {
            printf("%d Delete %d\n",++step,a);
            a--;
        }
        else if(dp[a][b]==dp[a][b-1]+1 && b>=1)
        {
            printf("%d Insert %d,%c\n",++step,a+1,s1[b-1]);
            b--;
        }
    }

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f

typedef long long ll;
using namespace std;

int ans;
string s;
string s1;
int dp[100][100];
int len2;
int len1;

void solve()
{
    int i,j;
    len2=s1.size();

    len1=s.size();

    memset(dp,0,sizeof(dp));

    for(j=0; j<=len1; j++)
    {
        dp[j][0]=j;
    }
    for(j=0; j<=len2; j++)
    {
        dp[0][j]=j;
    }

    for(i=1; i<=len1; i++)
    {

        for(j=1; j<=len2 ; j++)
        {

            if( s[i-1] == s1[j-1] )
                dp[i][j]=dp[i-1][j-1];
            else
                dp[i][j]=min( dp[i][j-1],min( dp[i-1][j] , dp[i-1][j-1] ) ) + 1;

        }
    }
    cout<<dp[ len1 ][ len2 ]<<endl;

    //for(i=1; i<=len1; i++)
    //cout<<dp[3][i]<<endl;

}
void out()
{
    int step=0,tmp;
    int a=len1,b=len2;
    while(a>=1 || b>=1)
    {
        if(s[a-1]==s1[b-1]) tmp=0;
        else tmp=1;


        if(dp[a][b]==dp[a-1][b-1]+tmp && a>=1 && b>=1)
        {
            if(tmp)
            {
                printf("%d Replace %d,%c\n",++step,a,s1[b-1]);
            }
                
            a--,b--;
        }

        else if(dp[a][b]==dp[a-1][b]+1 && a>=1)
        {
            printf("%d Delete %d\n",++step,a);
			a--;
        }

        else if(dp[a][b]==dp[a][b-1]+1 && b>=1)
        {
            printf("%d Insert %d,%c\n",++step,a+1,s1[b-1]);
			b--;
        }
    }

}
int main()
{


    while(cin>>s>>s1)
    {
        solve();
        out();
    }
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值