UVa #10723 Cyborg Genes (习题9-6)

86 篇文章 0 订阅
最长公共子序列模型。


不同的解出现于d[i-1][j] == d[i][j-1]


Run TIme: 0.009s

#define UVa  "9-6.10723.cpp"        //Cyborg Genes

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>

using namespace std;

int main() {
    const int maxn = 30+5;
    int T, n1, n2;
    scanf("%d", &T);
    getchar();
    for(int kase = 1; kase <= T; kase ++) {

        int d[maxn][maxn];
        int cnt[maxn][maxn];
        string str1, str2;
        getline(cin, str1);
        getline(cin, str2);
        n1 = str1.length();
        n2 = str2.length();


        for(int i = 0; i <= n1; i ++) {
            d[i][0] = 0;
            cnt[i][0] = 1;
        }
        for(int j = 0; j <= n2; j ++) {
            d[0][j] = 0;
            cnt[0][j] = 1;
        }

        for(int i = 1; i <= n1; i ++) {
            for(int j = 1; j <= n2; j ++) {
                int& u = d[i][j], &uc = cnt[i][j];
                if(str1[i-1] == str2[j-1]) {
                    u = d[i-1][j-1] + 1;
                    uc = cnt[i-1][j-1];
                }
                else {
                    int a = d[i-1][j], b = d[i][j-1];
                    u = max(a, b);
                    if(a > b)
                        uc = cnt[i-1][j];
                    else if(a < b)
                        uc = cnt[i][j-1];
                    else
                        uc = cnt[i-1][j] + cnt[i][j-1];
                }
            }
        }
        printf("Case #%d: %d %d\n", kase,  n1+n2-d[n1][n2], cnt[n1][n2]);
    }

    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值