字符串中的字符包含问题

假设有两个字符串contain,tobecontained,这两个字符串都是由字母和数字组成的,即构成这两个字符串的字符仅仅有a-z,A-Z,0-9这62个字符,当然,每个字符都可以出现若干次。

下面的算法实现了这样一个功能:当tobecontained字符串中的每个字符都出现在contain字符串中时,返回1;如果tobecontained字符串中的任何一个字符在contain字符串中没有找到时,返回0;如果出现了不在限定字符中的字符,返回-1。

 

在下面的实现中,out代表contain,in代表tobecontained

 

#define _FILE_OFFSET_BITS 64    //in 32-bit system, this macro can make sure that sizeof(off_t)=8
                                //in 64-bit system,sizeof(off_t)=8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ERR_CONTAIN -1  //error
#define IS_CONTAINED 1  //contained
#define NO_CONTAINED 0  //not contained

int isContained(const char *out, const size_t outlen, const char *in, const size_t inlen)
{
        off_t result = 0;
        off_t tmp = 0;
        size_t i = 0;
        char ch;
        if(NULL == out || NULL == in)
                return 0;
        for(i = 0; i < outlen; i++)
        {
                ch = out[i];
                if('A' <= ch && ch <= 'Z')
                        result = result | 1 << (ch - 'A');
                else if('a' <= ch && ch<= 'z')
                        result = result | 1 << (ch - 'a' + 26);
                else if('0' <= ch && ch <='9')
                        result = result | 1 << (ch - '0' + 52);
                else
                        return ERR_CONTAIN;
        }
        for(i = 0; i < inlen; i++)
        {
                ch = in[i];
                if('A' <= ch && ch <= 'Z')
                        tmp = result & 1 << (ch - 'A');
                else if('a' <= ch && ch<= 'z')
                        tmp = result & 1 << (ch - 'a' + 26);
                else if('0' <= ch && ch <='9')
                        tmp = result & 1 << (ch - '0' + 52);
                else
                        return ERR_CONTAIN;
                if(0 == tmp)
                        return NO_CONTAINED;
        }
        return IS_CONTAINED;
}

int main()
{
        char a[] = "abcdxyz019MKSJDXYZ2345";
        char b[] = "abcdxyz019XYZ";
        int res = isContained(a, strlen(a),b, strlen(b));
        if(ERR_CONTAIN == res )
                printf("error : %d\n",res);
        else
                printf("result : %d\n sizeof(off_t)=%d\n", res,sizeof(off_t));
}


isContained函数完成了字符是否包含的问题,时间复杂度为O(m+n),空间复杂度为O(1)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值