#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;
}
在一个字符串中找一个最大重复子串
最新推荐文章于 2022-11-28 22:10:27 发布