程序员面试宝典:输入一行字符串,找出其中出现的相同且长度最长的字符串----后缀数组求解...

更多关于最长子串:http://blog.youkuaiyun.com/szu030606/article/details/8088596

#include<iostream>
#include<string>
#include<string.h>
using namespace std;

int mycmp( const void * a, const void * b )
{
   return strcmp( *( char ** )a, *( char ** )b );

}

int cmp( char * str1, char * str2 )
{
  int len = 0;

  while( *str1++ == *str2++ )
  {
    ++len;
  }

  return len;
}

void sub_str( char ** a, int n )
{
  int max = 0;
  int len = 0;
  int index =0;
  int i;
  char * tmp;

  for( i = 0; i < n-1; ++i )
  {
    len = cmp( a[i],a[i+1] );

    if( len > max )
    {
      max = len;
      index=i;
    }
  }

  cout<<"index: "<<( n - strlen( a[index] ) ) + 1<<endl;
  //输出公共前驱
  tmp = a[index];
  while( max-- )
  {
    cout<<*tmp++;
   
  }

}

int main()
{
  char str[] ="yyabcdabjcabceg";
  char * a[100];
  unsigned int s_len = strlen( str );
  char * tmp = str;

/*记录子串首地址,子串之间只差一个字符*/
for( int i = 0; i < s_len; ++i, ++tmp )
{
  a[i] = tmp;
}

/*子串排序*/
qsort( a, s_len, sizeof(char *), mycmp );

/*求出相邻子串的公共前驱*/
sub_str( a, s_len );


system("pause");
return 0;
}

转载于:https://www.cnblogs.com/litana/archive/2013/03/31/2991328.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值