字符串中连续最长重复子串

使用后缀数组的概念。


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

const int max = 200; 

int scmp(const void *p, const void *q); 
int comlen(const char *p, const char *q); 

int main(void) 
{ 
	char c[max]; 
	char *a[max]; 
	int i,j,len,maxlen,maxi; 
  
	printf( "Please input the string:\n "); 
	if(fgets(c,max,stdin) == NULL) 
	{ 
		printf( "error!\n "); 
		exit(-1); 
	} 

	len = strlen(c) - 1; 

	if(c[len] == '\n ') 
	c[len] = '\0 '; 

	for(i = 0; i < len; i++) 
		a[i] = &c[i];               //提取后缀

	qsort(a, len - 1,sizeof(char *),scmp);  //对后缀进行排序
	maxlen = 0; 
	maxi = 0; 
	for(i = 0; i < len - 1; i++) 
	if((j= comlen(a[i],a[i+1])) > maxlen) 
	{ 
		maxlen = j; 
		maxi = i; 
	} 
	printf( "The max substring of this string is:\n "); 
	printf( "%.*s\n ",maxlen,a[maxi]); 

	return 0; 
} 


int scmp(const void *p, const void *q) 
{ 
	char *v1, *v2; 
	v1 = *(char **)p; 
	v2 = *(char **)q; 
	return strcmp(v1,v2); 
} 

int comlen(const char *p, const char *q) 
{ 
	int i = 0; 

	while((*p != '\0 ') && (*p++ == *q++)) 
	i++; 
	return i; 
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值