来自M78的光之文轩 2022-11-22 16:25 采纳率: 92%
浏览 2
已结题

递归的代码找不到错误


/*
/*
Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below:

Deletion: a letter in x is missing in y at a corresponding position.
Insertion: a letter in y is missing in x at a corresponding position.
Change: letters at corresponding positions are distinct
Certainly, we would like to minimize the number of all possible operations.

Illustration
A G T A A G T * A G G C

| | |       |   |   | |

A G T * C * T G A C G C
Deletion: * in the bottom line
Insertion: * in the top line
Change: when the letters at the top and bottom are distinct
This tells us that to transform x = AGTCTGACGC into y = AGTAAGTAGGC we would be required to perform 5 operations (2 changes, 2 deletions and 1 insertion). If we want to minimize the number operations, we should do it like

A  G  T  A  A  G  T  A  G  G  C

|  |  |        |     |     |  |

A  G  T  C  T  G  *  A  C  G  C
and 4 moves would be required (3 changes and 1 deletion).

In this problem we would always consider strings x and y to be fixed, such that the number of letters in x is m and the number of letters in y is n where n ≥ m.

Assign 1 as the cost of an operation performed. Otherwise, assign 0 if there is no operation performed.

Write a program that would minimize the number of possible operations to transform any string x into a string y.

Input
The input consists of the strings x and y prefixed by their respective lengths, which are within 1000.

Output
An integer representing the minimum number of possible operations to transform any string x into a string y.

Sample
Inputcopy
10 AGTCTGACGC
11 AGTAAGTAGGC
Outputcopy
4

*/
#include<iostream>
using namespace std;
char a[1010]={0},b[1010]={0};
int len1=0,len2=0,len3=0,i=0,m,n;
int dfs(int w,int e){
    if(b[e]=='\0') return m-w;  //如果b[]中所有字母都匹配了,删除a[]中剩下的字母 
    if(a[w]=='\0') return n-e;  //如果a[]中所有字母都匹匹配了,插入b[]中还未匹配的字母  
    for(;w<m&&e<n;w++,e++){  //a[w]与b[e]都为'\0'时,结束循环  
        if(a[w]==b[e]) {
            continue;   //如果相等,继续循环向前 
        }
        else{
            len1=dfs(w,e+1)+1;  //插入 
            len2=dfs(w+1,e)+1;  //删除 
            len3=dfs(w+1,e+1)+1;  //转换  
            if(len1<=len2 && len1<=len3) return len1;
            else if(len2<=len1 && len2<=len3) return len2;  //返回len1,len2,len3中的最小值 
            else return len3;
        }
    }
}
int main(){
    cin>>m;
    for(i=0;i<m;i++) cin>>a[i];
    cin>>n;
    for(i=0;i<n;i++) cin>>b[i];
    cout<<dfs(0,0);
    return 0;
}
  • 写回答

3条回答 默认 最新

  • 关注

    (1)for循环不是所有的代码段都有返回值,比如 ,输入的2个字符串 都是3 AAA,你这个代码只会执行continue语句,最后没有返回值
    (2)漏判了 a[w]和b[e]同时到达字符串终点的情况。
    代码做了下修改

    #include <iostream>
    using namespace std;
    char a[1010] = { 0 }, b[1010] = { 0 };
    int dp[1010][1010] ;
    int len1 = 0, len2 = 0, len3 = 0, i = 0,j, m, n;
    #define INT_MAX 999999999
    int dfs(int w, int e) {
    
        if (b[e] == '\0' && a[w] == '\0') return 0;
        else if (b[e] == '\0') { dp[w][e] = m - w; return m - w; } //如果b[]中所有字母都匹配了,删除a[]中剩下的字母 
        else if (a[w] == '\0') { dp[w][e] = n - e; return n - e; } //如果a[]中所有字母都匹匹配了,插入b[]中还未匹配的字母  
        else
        {
            if (dp[w][e] != INT_MAX)
                return dp[w][e];
    
            if (a[w] == b[e])
                dp[w][e] = dfs(w + 1, e + 1);
    
            len1 = dfs(w, (e + 1)) + 1;  //插入 
            dp[w][e] = len1 < dp[w][e] ? len1 : dp[w][e];
    
            len2 = dfs((w + 1), e) + 1;  //删除 
            dp[w][e] = len2 < dp[w][e] ? len2 : dp[w][e];
    
            len3 = dfs((w + 1), (e + 1)) + 1;  //转换 
            dp[w][e] = len3 < dp[w][e] ? len3 : dp[w][e];
            
            
            return dp[w][e];
        }
    }
    int main() {
        cin >> m;
        for (i = 0; i < m; i++) cin >> a[i];
        cin >> n;
        for (i = 0; i < n; i++) cin >> b[i];
    
        for (i = 0; i < m; i++)
            for (j = 0; j < n; j++)
                dp[i][j] = INT_MAX;
    
        cout << dfs(0, 0);
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月1日
  • 已采纳回答 11月23日
  • 修改了问题 11月22日
  • 创建了问题 11月22日

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址