六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 34|回复: 0

字符串生成“后缀数组”《编程珠玑》

[复制链接]

升级  18%

21

主题

21

主题

21

主题

秀才

Rank: 2

积分
77
 楼主| 发表于 2013-1-26 14:29:32 | 显示全部楼层 |阅读模式
 
#include<iostream>using namespace std;const int N=26;extern int com(const void *a,const void *b);int main(){  char *s="Gejun Com on C Come in";  if(strlen(s)<=26)  {    int i=0;char *as[N]={NULL};   while(*s!='\0')   { if(*s!=' ')     as[i++]=s;       s++;   }   int num=i;   qsort(as,num,sizeof(char*),com);      for(i=0;i<num;i++)   cout<<as[i]<<endl;  // cout<<*(char*)as[0]<<" "<<*(char*)as[1]<<endl;  }system("Pause");return 0;}int com(const void *a,const void *b){ //char t1=**(char**)a,t2=**(char**)b;//cout<<t1<<" "<<t2<<" for test"<<endl;  return strcmp(*(char**)a,*(char**)b);  } 上述代码的作用就是将字符串"Gejun Com on C Come in"转换成它的后缀数组,结果图:
 

C Come inCom on C Come inCome inGejun Com on C Come ine inejun Com on C Come ininjun Com on C Come inm on C Come inme innn C Come inn Com on C Come inom on C Come inome inon C Come inun Com on C Come in请按任意键继续. . .  后缀数组的作用是用来识别字符处的最长重复子字符串!
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表