zoukankan      html  css  js  c++  java
  • hdu 1516 String Distance and Transform Process

    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

    代码
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    char s1[1005], s2[1005];
    int dp[1005][1005];
    int main()
    {
        int len1, len2, i, j, t;
        while (~scanf("%s%s", s1, s2)){
            len1 = strlen(s1); len2 = strlen(s2);
            for (i = len1; i >= 1; i--)
                s1[i] = s1[i - 1];
            for (i = len2; i >= 1; i--)
                s2[i] = s2[i - 1];
            for (i = 0; i <= len1; i++)
                for (j = 0; j <= len2; j++)
                {
                    if (i == 0 && j == 0) dp[i][j] = 0;
                    else if (i == 0) dp[i][j] = j;
                    else if (j == 0) dp[i][j] = i;
                    else {
                        if (s1[i] == s2[j]) dp[i][j] = dp[i - 1][j - 1];
                        else dp[i][j] = dp[i - 1][j - 1] + 1;
                        dp[i][j] = min(dp[i][j], min(dp[i - 1][j], dp[i][j - 1]) + 1);
    
                    }
                }
            printf("%d
    ", dp[len1][len2]);
            t = 0; 
            i = len1; 
            j = len2;
            while (i > 0 || j > 0)
            {
                if (s1[i] == s2[j] && dp[i][j] == dp[i - 1][j - 1]){
                    i--; 
                    j--; 
                    continue;
    
                }
                t++;
                printf("%d ", t);
                if (j > 0 && dp[i][j] == dp[i][j - 1] + 1){
                    printf("Insert %d,%c
    ", i + 1, s2[j]);
                    j--;
    
                }
                else if (i > 0 && dp[i][j] == dp[i - 1][j] + 1){
                    printf("Delete %d
    ", i);
                    i--;
    
                }
                else if (dp[i][j] == dp[i - 1][j - 1] + 1){
                    printf("Replace %d,%c
    ", i, s2[j]);
                    i--; 
                    j--;
    
                }
            }
        }
        system("pause");
        return 0;
    }


  • 相关阅读:
    php 本地 备份远程mysql和mdb 多任务只执行一次
    c# 监控服务器上传木马(包含可疑文件)
    jquery 简短 右键菜单 兼容ie6 ie7 ie8 firefox chrome
    分解从身份证中读回的户籍地址
    备份mysql(一表一文件)
    THINKPHP 3.0 整合KINDEDITOR 4.05
    c# 仿照计划任务(定时提示、定时运行程序、定时打开url(前台/后台))/每天/每周/每月/一次 多时间段
    php基本操作echo
    c# 扫描可疑文件(找到木马)(简)
    GOOGLE 地图,查询地名,移动标记,生成静态地图
  • 原文地址:https://www.cnblogs.com/pcdl/p/13332864.html
Copyright © 2011-2022 走看看