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

    http://acm.hdu.edu.cn/showproblem.php?pid=1516

    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可以求得编辑距离,再从后面开始打印可以打印出路径。构造一个二位数组,dp[i][j] 表示 长度为 i 的 A序列,变成长度为 j 的 B 序列要做的操作数。(注意 i j 可以为0 即 空序列)

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 char str1[100],str2[100];
    10 int dp[100][100];
    11 int len1,len2;
    12 
    13 
    14 void solve()
    15 {
    16     int a=len1,b=len2;
    17     int c=dp[a][b];
    18 //    printf("a=%d b=%d c=%d
    ",a,b,c);
    19     int index=0;
    20     while(a>0||b>0)
    21     {
    22         if(a==0&&b>0)
    23         {
    24             printf("%d Insert 1,%c
    ",++index,str2[b-1]);
    25             b--;
    26             continue;
    27         }
    28         else if(a>0&&b==0)
    29         {
    30             printf("%d Delete %d
    ",++index,a);
    31             a--;
    32             continue;
    33         }
    34         else
    35         {
    36             if(c==dp[a-1][b-1]&&str1[a-1]==str2[b-1])
    37             {
    38                 a--;b--; 
    39             }
    40             else if(c==dp[a-1][b-1]+1)
    41             {
    42                 printf("%d Replace %d,%c
    ",++index,a,str2[b-1]);
    43                 c--;a--;b--;
    44             }
    45             //后两个else是根据题解改的,不是很理解 
    46             else if(c==dp[a][b-1]+1)
    47             {
    48                 printf("%d Insert %d,%c
    ",++index,a+1,str2[b-1]);
    49                 c--;b--;
    50             }
    51             else if(c==dp[a-1][b]+1)
    52             {
    53                 printf("%d Delete %d
    ",++index,a);
    54                 c--;a--;
    55             }
    56         }
    57     }
    58     return ;
    59 }
    60 
    61 int main()
    62 {
    63     freopen("sample.txt","r",stdin);
    64     while(~scanf("%s %s",str1,str2))
    65     {
    66         getchar();
    67         memset(dp,0,sizeof(dp));
    68         len1=strlen(str1);
    69         len2=strlen(str2);
    70         for(int i=1;i<=len1;i++)
    71         {
    72             dp[i][0]=i;
    73         }
    74         for(int i=1;i<=len2;i++)
    75         {
    76             dp[0][i]=i;
    77         }
    78         for(int i=1;i<=len1;i++)
    79         {
    80             for(int j=1;j<=len2;j++)
    81             {
    82                 int t=1;
    83                 if(str1[i-1]==str2[j-1])
    84                     t=0;
    85                 dp[i][j]=dp[i-1][j-1]+t;
    86                 dp[i][j]=min(dp[i][j],dp[i-1][j]+1);
    87                 dp[i][j]=min(dp[i][j],dp[i][j-1]+1);
    88 //                printf("dp[%d][%d]=%d
    ",i,j,dp[i][j]);
    89             }
    90         }
    91         printf("%d
    ",dp[len1][len2]);
    92         solve();
    93     }
    94     return 0;
    95 }
     
  • 相关阅读:
    Laravel 5.2 使用 JWT 完成多用户认证 | Laravel China 社区
    (上线时清缓存)laravel 5.1 的程序性能优化(配置文件)
    linux计划任务及压缩归档
    用户及用户管理
    vim编辑器
    linux进阶命令
    权限管理
    linux基础命令2
    linu基础命令1
    连接Xshell
  • 原文地址:https://www.cnblogs.com/jiamian/p/11233370.html
Copyright © 2011-2022 走看看