在一个字符串中找一个最大重复子串

#include <iostream.h>
bool find_sub_string(const char * str,int str_length,int * offset,int * len){
	int start=0;//每一次查询的开始处
	int off=0;//查询到的偏移量
	int maxlen=1;//查询到的最大长度
	int inmaxlen=1;
	int inlen=1;//每次循环查找到的最大长度
	int i;//for循环的变量
	for(;start+inmaxlen<str_length;start+=inmaxlen){//每次查找偏移量+上次查找到的最大长度<总长度 为循环条件
		for(i=start+1,inmaxlen=1,inlen=0;i<str_length;i++){
			if(str[start+inlen]==str[i]){
				inlen++;
			}
			else{//当对应的字符不同时,判断新找到的是否更长
				if(inlen>inmaxlen){//如果找到了一个新的更长的字符串
					inmaxlen=inlen;
				}
				inlen =0;//inlen重新开始
			}
		}
		if(inmaxlen>maxlen){
			off=start;
			maxlen=inmaxlen;
		}
	}
	if(maxlen<2)//没有找到
		return false;
	//找到了
	*offset=off;
	*len=maxlen;
	return true;
}
int main(int argc, char* argv[])
{
	int offset,len;
	if(find_sub_string("ttabcftrgabcd",13,&offset,&len)){//abcxyzbcxyzbx
		cout<<offset<<"  "<<len<<endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值