最小子串(改进版)

给一篇文章,里面是由一个个单词组成,单词中间空格隔开,再给一个字符串指针数组,比如 char *str[]={"hello","world","good"};

求文章中包含这个字符串指针数组的最小子串。注意,只要包含即可,没有顺序要求。

提示:文章也可以理解为一个大的字符串数组,单词之前只有空格,没有标点符号。

#include <stdio.h>
#include <stdlib.h>

/*
 *截取字符串
 */
char* subString(char *src, int pos, int len) {
	char *p = src;
	char *substr = (char*) calloc(sizeof(char), len + 1);
	int i = 0;
	p += pos;
	while (len--) {
		substr[i++] = *(p++);
	}
	substr[i] = '\0';
	return substr;
}

int* getIndexs(char *content, char* search[], int searchSize) {
	int i = 0;
	int rear = 0, front = 0;

	int contentSize = strlen(content);

	int* hash = calloc(sizeof(int), contentSize);
	for (i = 0; i < contentSize; i++) {
		hash[i] = -1;
	}

	i = 0;
	int myLen = strlen(search[i++]);
	int maxLen = myLen, minLen = myLen;

	i = 0;
	while (i < searchSize) {
		myLen = strlen(search[i++]);
		minLen = minLen > myLen ? myLen : minLen;
		maxLen = maxLen < myLen ? myLen : maxLen;
	}

	while (rear <= contentSize) {
		if (*(content + rear) == ' ' || *(content + rear) == '\0') {
			int difLen = rear - front;
			//单词长度在区间内
			if (difLen <= maxLen && difLen >= minLen) {
				for (i = 0; i < searchSize; i++) {
					if (!strcmp(search[i], subString(content, front, difLen))) {
						hash[front] = i;
					}
				}
			}
			rear += 1;
			front = rear;
		} else {
			rear++;
		}
	}
	return hash;
}

char* getStrByArrage(char* content, int begin, int end) {
	char* p = content + end;
	int rear = end;
	while (*(p++) != ' ') {
		rear++;
	}
	return subString(content, begin, rear - begin);
}

char* findMinSubString(char* content, char* search[], int rows) {
	int contentSize = strlen(content);
	int* hash = getIndexs(content, search, rows);

	int i;
	/*for (i = 0; i < contentSize; ++i) {
	 if (hash[i] != -1)
	 printf("front:%3d, searchId:%3d", i, hash[i]);
	 }*/

	//当前范围下搜索字出现次数
	int appearNum[rows];
	for (i = 0; i < rows; i++) {
		appearNum[i] = 0;
	}

	//起止游标
	int prev = 0, end = 0;

	//最小长度
	int minLen = 2100000000;

	//最小的起止下标
	int minPrev = 0, minEnd = 0;

	//已出现的搜索词数量
	int count = 0;

	while (end < contentSize || count == rows) {
		//prev,end不包含所有关键字,扩大范围
		if (count < rows) {
			if (hash[end] != -1) {
				if (appearNum[hash[end]] == 0) {
					count++;
				}

				appearNum[hash[end]]++;
			}
			end++;
		}
		//包含所有关键字,缩小范围
		if (count == rows) {
			if (end - prev < minLen) {
				minPrev = prev;
				minEnd = end - 1;
				minLen = end - prev;
			}

			if (hash[prev] != -1) {
				appearNum[hash[prev]]--;
				if (appearNum[hash[prev]] == 0) {
					count--;
				}
			}
			prev++;
		}

	}

	return getStrByArrage(content, minPrev, minEnd);

}

int main(void) {
	//文章内容
	char* content =
			"hello good world kitty my world is a hello good a world hello space";
	char *search[] = { "hello", "world", "good" };

	int rows = sizeof(search) / sizeof(char*);

	int contentSize = strlen(content);
	puts(findMinSubString(content, search, rows));
	return EXIT_SUCCESS;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值