字符串中的字符包含问题
来源:互联网 发布:从零开始学淘宝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
- 字符串中的字符包含问题
- 检查字符串中是否包含某字符集合中的字符
- 字符串字符包含
- ios 字符串包含某个字符
- 001_008 Python 检查字符串中是否包含某字符集合中的字符
- 字符串包含问题
- 字符串移位包含问题
- 字符串的包含问题
- 字符串是否包含问题
- 字符串包含问题
- 字符串包含问题
- 字符串移位包含问题
- 字符串包含问题
- 字符串移位包含问题
- 字符串移位包含问题
- 字符串包含问题
- 字符串移位包含问题
- 字符串移位包含问题
- 我的2013
- 安卓UDP通信
- 利用函数传递结构体--冒泡排序
- phpredis中文手册——《redis中文手册》 php版
- tr1::bind与tr1::function的用法与具体应用
- 字符串中的字符包含问题
- C#中计时器Timer的三种使用方法
- c/c++各种字符、字符串类型转换
- JDBC连接各种数据库方法
- 互联网(internet)又称因特网
- C#数字图像处理有3种典型方法:提取像素法、内存法、指针法。
- socket穿透代理代码(C++版)
- 你知道这些游戏都在阿里云上吗?
- 采购退货从接收退货和从库存退货区别