字符串中的字符包含问题

来源:互联网 发布:从零开始学淘宝txt 编辑:程序博客网 时间:2024/04/28 15:27

假设有两个字符串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 containedint 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
原创粉丝点击