最长重复子串问题

wangdong20 2013-09-22 02:14:16
求大神指点
想要把一个字符串中重复最长的部分提取出来,比如说有如下字符串:
abcdbcdbcb
对于这个字符串最长的重复子串为bcdbc!

查了下网上的解答,代码如下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXCHAR 5000 //最长处理5000个字符

char c[MAXCHAR], *a[MAXCHAR];

int comlen( char *p, char *q ){
int i = 0;
while( *p && (*p++ == *q++) )
++i;
return i;
}

int pstrcmp( const void *p1, const void *p2 ){ // 这个函数是什么意思,参数看不懂
return strcmp( *(char* const *)p1, *(char* const*)p2 );
}

int main( ){
char ch;
int n=0;
int i, temp;
int maxlen=0, maxi=0;
printf("Please input your string:\n");
while( (ch=getchar())!='\n' ){
a[n]=&c[n];
c[n++]=ch;
}
c[n]='\0';
qsort( a, n, sizeof(char*), pstrcmp ); // 这里的快速排序是对什么排序
for(i=0; i<n-1; ++i ){
temp=comlen( a[i], a[i+1] );
if( temp>maxlen ){
maxlen=temp;
maxi=i;
}
}
printf("%.*s\n",maxlen, a[maxi]);
system("PAUSE");
return 0;
}

但是这个代码看不懂,求大神指点
...全文
243 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wopabe 2013-09-27
  • 打赏
  • 举报
回复
真是高手啊,学习了
赵4老师 2013-09-23
  • 打赏
  • 举报
回复
引用 9 楼 wangdong20 的回复:
[quote=引用 4 楼 zhao4zhong1 的回复:] int pstrcmp( const void *p1, const void *p2 ){//该函数是qsort函数的“回调”函数。两个参数的类型为指向常量的无类型指针 return strcmp( *(char* const *)p1, *(char* const*)p2 );//将参数强制类型转换为char* const *类型,然后取其值,其值为char* const类型。 }
pstrcmp为什么在qsort里面没有参数[/quote] pstrcmp如果在调用qsort时有参数,和调用pstrcmp将其返回值作为实际参数怎么区别呢?
lm_whales 2013-09-23
  • 打赏
  • 举报
回复
引用 9 楼 wangdong20 的回复:
[quote=引用 4 楼 zhao4zhong1 的回复:] int pstrcmp( const void *p1, const void *p2 ){//该函数是qsort函数的“回调”函数。两个参数的类型为指向常量的无类型指针 return strcmp( *(char* const *)p1, *(char* const*)p2 );//将参数强制类型转换为char* const *类型,然后取其值,其值为char* const类型。 }
pstrcmp为什么在qsort里面没有参数[/quote] 函数指针,这时windows 回调函数 的传递方式 对于C 就是函数指针,实参用函数地址,而函数名可以代表它的地址。 qsort 的实现代码里会通过函数指针,调用这个函数。 qsort VC有源码,当比较元素时,会调用实参 ,调用时比较函数的参数就是,被比较的两个数据.
wangdong20 2013-09-23
  • 打赏
  • 举报
回复
弱弱的问一句,java里面有没有类似的qsort()方法
wangdong20 2013-09-22
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
int pstrcmp( const void *p1, const void *p2 ){//该函数是qsort函数的“回调”函数。两个参数的类型为指向常量的无类型指针 return strcmp( *(char* const *)p1, *(char* const*)p2 );//将参数强制类型转换为char* const *类型,然后取其值,其值为char* const类型。 }
pstrcmp为什么在qsort里面没有参数
max_min_ 2013-09-22
  • 打赏
  • 举报
回复
引用 3 楼 wangdong20 的回复:
[quote=引用 2 楼 zhao4zhong1 的回复:] qsort Performs a quick sort. void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) ); Routine Required Header Compatibility qsort <stdlib.h> and <search.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value None Parameters base Start of target array num Array size in elements width Element size in bytes compare Comparison function elem1 Pointer to the key for the search elem2 Pointer to the array element to be compared with the key Remarks The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call: compare( (void *) elem1, (void *) elem2 ); The routine must compare the elements, then return one of the following values: Return Value Description < 0 elem1 less than elem2 0 elem1 equivalent to elem2 > 0 elem1 greater than elem2 The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function. Example /* QSORT.C: This program reads the command-line * parameters and uses qsort to sort them. It * then displays the sorted arguments. */ #include <stdlib.h> #include <string.h> #include <stdio.h> int compare( const void *arg1, const void *arg2 ); void main( int argc, char **argv ) { int i; /* Eliminate argv[0] from sort: */ argv++; argc--; /* Sort remaining args using Quicksort algorithm: */ qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare ); /* Output sorted list: */ for( i = 0; i < argc; ++i ) printf( "%s ", argv[i] ); printf( "\n" ); } int compare( const void *arg1, const void *arg2 ) { /* Compare all of both strings: */ return _stricmp( * ( char** ) arg1, * ( char** ) arg2 ); } Output [C:\code]qsort every good boy deserves favor boy deserves every favor good Searching and Sorting Routines See Also bsearch, _lsearch
赵老师来了,那就好办了,赵老师能解释下
int pstrcmp( const void *p1, const void *p2 ){    // 这个函数是什么意思,参数看不懂
    return strcmp( *(char* const *)p1, *(char* const*)p2 );
}
是什么意思吗[/quote] 封装库函数接口而已!
wangdong20 2013-09-22
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
int pstrcmp( const void *p1, const void *p2 ){//该函数是qsort函数的“回调”函数。两个参数的类型为指向常量的无类型指针 return strcmp( *(char* const *)p1, *(char* const*)p2 );//将参数强制类型转换为char* const *类型,然后取其值,其值为char* const类型。 }
有的类似于Java的泛型Compareable接口
赵4老师 2013-09-22
  • 打赏
  • 举报
回复
char c[MAXCHAR], *a[MAXCHAR]; a为一个数组,该数组的每个元素的类型是char *
赵4老师 2013-09-22
  • 打赏
  • 举报
回复
int pstrcmp( const void *p1, const void *p2 ){//该函数是qsort函数的“回调”函数。两个参数的类型为指向常量的无类型指针 return strcmp( *(char* const *)p1, *(char* const*)p2 );//将参数强制类型转换为char* const *类型,然后取其值,其值为char* const类型。 }
wangdong20 2013-09-22
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
qsort Performs a quick sort. void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) ); Routine Required Header Compatibility qsort <stdlib.h> and <search.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value None Parameters base Start of target array num Array size in elements width Element size in bytes compare Comparison function elem1 Pointer to the key for the search elem2 Pointer to the array element to be compared with the key Remarks The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call: compare( (void *) elem1, (void *) elem2 ); The routine must compare the elements, then return one of the following values: Return Value Description < 0 elem1 less than elem2 0 elem1 equivalent to elem2 > 0 elem1 greater than elem2 The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function. Example /* QSORT.C: This program reads the command-line * parameters and uses qsort to sort them. It * then displays the sorted arguments. */ #include <stdlib.h> #include <string.h> #include <stdio.h> int compare( const void *arg1, const void *arg2 ); void main( int argc, char **argv ) { int i; /* Eliminate argv[0] from sort: */ argv++; argc--; /* Sort remaining args using Quicksort algorithm: */ qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare ); /* Output sorted list: */ for( i = 0; i < argc; ++i ) printf( "%s ", argv[i] ); printf( "\n" ); } int compare( const void *arg1, const void *arg2 ) { /* Compare all of both strings: */ return _stricmp( * ( char** ) arg1, * ( char** ) arg2 ); } Output [C:\code]qsort every good boy deserves favor boy deserves every favor good Searching and Sorting Routines See Also bsearch, _lsearch
赵老师来了,那就好办了,赵老师能解释下
int pstrcmp( const void *p1, const void *p2 ){    // 这个函数是什么意思,参数看不懂
    return strcmp( *(char* const *)p1, *(char* const*)p2 );
}
是什么意思吗
赵4老师 2013-09-22
  • 打赏
  • 举报
回复
qsort Performs a quick sort. void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) ); Routine Required Header Compatibility qsort <stdlib.h> and <search.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value None Parameters base Start of target array num Array size in elements width Element size in bytes compare Comparison function elem1 Pointer to the key for the search elem2 Pointer to the array element to be compared with the key Remarks The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call: compare( (void *) elem1, (void *) elem2 ); The routine must compare the elements, then return one of the following values: Return Value Description < 0 elem1 less than elem2 0 elem1 equivalent to elem2 > 0 elem1 greater than elem2 The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function. Example /* QSORT.C: This program reads the command-line * parameters and uses qsort to sort them. It * then displays the sorted arguments. */ #include <stdlib.h> #include <string.h> #include <stdio.h> int compare( const void *arg1, const void *arg2 ); void main( int argc, char **argv ) { int i; /* Eliminate argv[0] from sort: */ argv++; argc--; /* Sort remaining args using Quicksort algorithm: */ qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare ); /* Output sorted list: */ for( i = 0; i < argc; ++i ) printf( "%s ", argv[i] ); printf( "\n" ); } int compare( const void *arg1, const void *arg2 ) { /* Compare all of both strings: */ return _stricmp( * ( char** ) arg1, * ( char** ) arg2 ); } Output [C:\code]qsort every good boy deserves favor boy deserves every favor good Searching and Sorting Routines See Also bsearch, _lsearch
wangdong20 2013-09-22
  • 打赏
  • 举报
回复
在执行到
qsort();之前的c[n] = '\0';
a里面的元素如下

执行完qsort()之后,就变成

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧