后缀数组模板~~~

int n,k;
int sa[MaxN+10],rank[MaxN+10],tmp[MaxN+10],height[MaxN+10];
bool compare_sa(int i,int j){
	 if(rank[i] != rank[j] )return rank[i] < rank[j];
	 else{
  	 	 int ri = i + k <= n ? rank[i+k] : -1; 
  	 	 int rj = j + k <= n ? rank[j+k] : -1;
		 return ri < rj;	 
     }	 
}
void construct_sa(char *s,int *sa){
	 n = strlen(s);
	 repf(i,0,n){sa[i]=i;rank[i]=i<n?s[i]:-1;}
	 for(k=1;k<=n;k*=2){
		sort(sa,sa+n+1,compare_sa);
		
		tmp[sa[0]] = 0;
		repf(i,1,n) tmp[sa[i]]=tmp[sa[i-1]] + (compare_sa(sa[i-1],sa[i])?1:0);
		repf(i,0,n) rank[i]=tmp[i];
	 }
}
void construct_lcp(char *s,int *sa,int *lcp){
	  n=strlen(s);
	 repd(i,n)rank[sa[i]] = i;
	 int h=0;
	 lcp[0]=0;
	 rep(i,n){
  	    int  j =sa[rank[i] -1];
  	    
  	    if(h>0) h--;
  	    for(;j+h<n&&i+h<n;h++) if(s[j+h]!=s[i+h])break;
  	    
  	    lcp[rank[i]-1] = h;
  	 }
}

  模板题目 : POJ 2774 与2217~~~拼接两个字符串,求出lcp~~分别属于两个字符串的sa[i] 与sa[i+1]的最大height为最长公共子串~~~

 

posted @ 2013-10-10 13:42  UnkelTao  阅读(153)  评论(0编辑  收藏  举报