文件碎片-uva10132

//字符串子串朴素查找

int searchSubStr(char aSub[],char b[])

{

    int temp = 0;

    size_t len = 0;

    if (strlen(b)-strlen(aSub) > 0)

    {

        len = strlen(b)-strlen(aSub);

    }

    else if (strlen(b)-strlen(aSub) == 0)

    {

        len = strlen(b);

    }

    

    for (int i = 0;i < len; ++i)

    {

        int j = 0;

        while ((j < strlen(aSub))&&(b[i + j] == aSub[j]))

        {

            j++;

        }

        if (j == strlen(aSub))

        {

            return 0;

        }

        if ((j > 0)&&(j > temp))

        {

            temp = i;

        }

    }

    

    if (temp)

    {

        return temp;

    }

    return -1;

}

//字符先处理

void searchSame(char a[][20],int n)

{

    char tempA[1000];

    char tempB[1000];

    char tempResult[1000];

    memset(tempA, 0,sizeof(tempA));

    memset(tempB, 0,sizeof(tempB));

    memset(tempResult, 0,sizeof(tempResult));

    

    for (int i = 0;i < n; ++i)

    {

        memset(tempA, 0,sizeof(tempA));

        if (i == 0)

        {

            strcpy(tempA, a[i]);

        }

        else

        {

            strcpy(tempA, tempResult);

        }

        int subLen = (int)strlen(a[i]);

        for (int j = i+1;j < n;++j)

        {

            int len = (int)strlen(a[j]);

            memset(tempB, 0,sizeof(tempB));

            strcpy(tempB, a[j]);

            if (subLen > len)

            {//字符交换

                char tempC[1000];

                memset(tempC, 0,sizeof(tempC));

                strcpy(tempC, tempA);

                memset(tempA, 0,sizeof(tempA));

                strcpy(tempA, tempB);

                memset(tempB, 0,sizeof(tempB));

                strcpy(tempB, tempC);

            }

            

            if (strcmp(tempA, tempB) == 0)

            {

                continue;

            }

            

            int findex = searchSubStr(tempA,tempB);

            if (findex > 0)

            {

                strcat(tempResult, tempB+findex+1);

                break;

            }

            else if (findex == 0)

            {

                if (strlen(tempResult) < strlen(tempB))

                {

                    strcpy(tempResult, tempB);

                }

                break;

            }

        }

    }

    printf("%s",tempResult);

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值