求两个集合的最大公共连续子集&求指定子集和的子集

概述

1、求两个集合的最大公共连续子集
2、求指定子集和的子集

思路

1、求两个集合的最大公共连续子集
使用回溯法
2、求指定子集和的子集
使用动态规划法

源码

本文主要是以C、C++、QT为基础进行编程,运行前简单修改即可。测试入口函数为 void Test_MaxSubset()。

#include "QTime"
#include "QList"


static const quint32 SET1_ACOUNT = 100;//集合1的数量
static const quint32 SET1_VALUE_MAX = 9;
static quint32 gSet1[SET1_ACOUNT];

static const quint32 SET2_ACOUNT = 60;//集合2的数量
static const quint32 SET2_VALUE_MAX = 9;
static quint32 gSet2[SET2_ACOUNT];

static QList<quint32> gList;


void MaxSubset_OutputSet()
{
    quint32 loop = 0;

    printf("\nSet1:");
    while(loop<SET1_ACOUNT)
    {
        printf("%d ",gSet1[loop]);
        loop++;
    }

    printf("\nSet2:");
    loop = 0;
    while(loop<SET2_ACOUNT)
    {
        printf("%d ",gSet2[loop]);
        loop++;
    }
    printf("\n");
}
void MaxSubset_OutputSet1(quint32 outIndex, quint32 outLen)
{
    quint32 loop = outIndex;

    printf("\nSet1: Index=%d, Len=%d\n", outIndex, outLen);
    while(loop<SET1_ACOUNT && loop<(outLen+outIndex))
    {
        printf("%d ",gSet1[loop]);
        loop++;
    }

    printf("\n");
}
void MaxSubset_OutputSet2(quint32 outIndex, quint32 outLen)
{
    quint32 loop = outIndex;

    printf("\nSet2: Index=%d, Len=%d\n", outIndex, outLen);
    while(loop<SET2_ACOUNT && loop<(outLen+outIndex))
    {
        printf("%d ",gSet2[loop]);
        loop++;
    }

    printf("\n");
}

void MaxSubset_InitSet()
{
    quint32 loop = 0;
    QTime time = QTime::currentTime();

    qsrand(time.msec());
    while(loop<SET1_ACOUNT)
    {
        gSet1[loop] = qrand()%SET1_VALUE_MAX +1;
        loop++;
    }

    loop = 0;
    while(loop<SET2_ACOUNT)
    {
        gSet2[loop] = qrand()%SET2_VALUE_MAX +1;
        loop++;
    }
}
/*
note:获取两个集合的最大公共(连续)子集
return:子集长度
*/
quint32 MaxSubset_GetMaxSameContinuousSubset()
{
    quint32 i,j,tempLen;
    quint32 index1=0,index2=0,maxLen=0;

    for(i=0;i<SET1_ACOUNT;i++)
    {
        for(j=0;j<SET2_ACOUNT;j++)
        {
            tempLen = 0;
            while((i+tempLen)< SET1_ACOUNT
                  && (j+tempLen)< SET2_ACOUNT
                  && gSet1[i+tempLen] == gSet2[j+tempLen])
            {
                tempLen++;
            }

            if(tempLen > maxLen)
            {
                maxLen = tempLen;
                index1 = i;
                index2 = j;
            }
        }
    }

    if(maxLen != 0)
    {
        MaxSubset_OutputSet1(index1, maxLen);
        MaxSubset_OutputSet2(index2, maxLen);
    }
    return maxLen;
}


/*
* 获取集合1的子集,限制条件:子集和为 sum
* 类似背包问题
*/
quint32 MaxSubset_Set1SubsetSum_GetSum(quint32 endIndex, quint32 targetSum)
{
    quint32 s1=0,s2=0;

    if(endIndex>=SET1_ACOUNT || 0==targetSum)
    {
        return 0;
    }

    //qDebug()<<"endIndex:"<<endIndex<<"targetSum:"<<targetSum;
    if(targetSum >= gSet1[endIndex])
    {
        s1 = gSet1[endIndex];
        if(endIndex > 0)
        {
            s1 += MaxSubset_Set1SubsetSum_GetSum(
                        endIndex-1, targetSum-gSet1[endIndex]);
        }
    }
    if(s1 == targetSum)
    {
        return s1;
    }

    if(endIndex > 0)
    {
        s2 = MaxSubset_Set1SubsetSum_GetSum(endIndex-1, targetSum);
    }
    if(s2 == targetSum)
    {
        return s2;
    }

    return 0;
}
bool MaxSubset_Set1SubsetSum_GetItem(quint32 Set1acount, quint32 targetSum)
{
    qint32 i=Set1acount-1;
    quint32 s;
    bool ok=false;

    while(i>=0)
    {
        if(0 == targetSum)
        {
            break;
        }
        if(targetSum < gSet1[i])
        {
            i--;
            continue;
        }

        s = gSet1[i] +
            MaxSubset_Set1SubsetSum_GetSum(i-1, targetSum-gSet1[i]);
        if(s == targetSum)
        {
            qDebug()<<"["<<i<<"]="<<gSet1[i];
            targetSum -= gSet1[i];
            ok = true;
        }
        i--;
    }

    return ok;
}

void Test_MaxSubset()
{
    MaxSubset_InitSet();
    MaxSubset_OutputSet();

    qDebug()<<"MaxSubset_GetMaxSameContinuousSubset:";
    MaxSubset_GetMaxSameContinuousSubset();

    qDebug()<<"MaxSubset_Set1SubsetSum_GetItem:";
    if(MaxSubset_Set1SubsetSum_GetItem(SET1_ACOUNT, 30))
    {
        qDebug()<<"MaxSubset_Set1SubsetSum_GetItem Succ";
    }
}

运行结果

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值