标题:最长公共子序列
只看楼主
Buger
Rank: 1
等 级:新手上路
帖 子:60
专家分:7
注 册:2013-3-20
结帖率:84.62%
 问题点数:0 回复次数:0 
最长公共子序列
#include <stdio.h>
#include <string.h>
void LCSLength(int m, int n, char x[20], char y[20], int c[30][30], int b[30][30]) {
    int i, j;
    for(i = 1; i <= m; i++) c[i][0] = 0;
    for(i = 1; i <= n; i++) c[0][i] = 0;
    for(i = 1; i <= m; i++)
        for(j = 1; j <= n; j++)
            if(x[i] == y[j]) {
                c[i][j] = c[i - 1][j - 1] + 1;
                b[i][j] = 1;
            }
            else if(c[i - 1][j] >= c[i][j - 1]) {
                c[i][j] = c[i - 1][j];
                b[i][j] = 2;
            }
            else {
                c[i][j] = c[i][j - 1];
                b[i][j] = 3;
            }
}
void LCS(int i, int j, char x[20], int b[30][30]) {
    if(i == 0 || j == 0) return ;
    if(b[i][j] == 1) {
        LCS(i - 1, j - 1, x, b);
        printf("%c", x[i]);
    }
    else if(b[i][j] == 2) LCS(i - 1, j, x, b);
    else LCS(i, j - 1, x, b);
}
int main() {
    char x[] = "abcdefghjhj", y[] = "abcdegd";
    int n, m, b[30][30], c[30][30];
    m = strlen(x); n = strlen(y);
    LCSLength(m, n, x, y, c, b);
    LCS(m, n, x, b);
    return 0;
}
怎么不对啊!???????????
搜索更多相关主题的帖子: void include 
2013-05-09 21:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-406667-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041983 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved