BZOJ 2882 & 접미사 수조의 바보 실현

3088 단어
제목:
사전의 순서를 최소화하기 위한 문자 고리
SOL:
접미사 배열...끊겼다니...10sec의 실현. 저 10936ms...끊겼어???
랭크 3...라라라라라라라...
이산화를 바꾸면 좀 빠르지 않을까요?
Code:
  
/*==========================================================================
# Last modified: 2016-03-19 14:38
# Filename: 2882.cpp
# Description: 
==========================================================================*/
#define me AcrossTheSky 
#include <cstdio> 
#include <cmath> 
#include <ctime> 
#include <string> 
#include <cstring> 
#include <cstdlib> 
#include <iostream> 
#include <algorithm> 
  
#include <set> 
#include <map> 
#include <stack> 
#include <queue> 
#include <vector> 
 
#define lowbit(x) (x)&(-x) 
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++) 
#define FORP(i,a,b) for(int i=(a);i<=(b);i++) 
#define FORM(i,a,b) for(int i=(a);i>=(b);i--) 
#define ls(a,b) (((a)+(b)) << 1) 
#define rs(a,b) (((a)+(b)) >> 1) 
#define getlc(a) ch[(a)][0] 
#define getrc(a) ch[(a)][1] 
 
#define maxn 700000 
#define maxm 100000 
#define pi 3.1415926535898 
#define _e 2.718281828459 
#define INF 1070000000 
using namespace std; 
typedef long long ll; 
typedef unsigned long long ull; 
 
template<class T> inline 
void read(T& num) { 
    bool start=false,neg=false; 
    char c; 
    num=0; 
    while((c=getchar())!=EOF) { 
        if(c=='-') start=neg=true; 
        else if(c>='0' && c<='9') { 
            start=true; 
            num=num*10+c-'0'; 
        } else if(start) break; 
    } 
    if(neg) num=-num; 
} 
/*==================split line==================*/ 
int s[maxn];
int t[maxn],t2[maxn],c[maxn],sa[maxn];
int n;

void build_sa(int m){
	int *x=t,*y=t2;
	FORP(i,0,m) c[i]=0;
	FORP(i,0,n-1) c[x[i]=s[i]]++;
	FORP(i,1,m) c[i]+=c[i-1];
	FORM(i,n-1,0) sa[--c[x[i]]]=i;
	for (int k=1;k<=n;k <<= 1){
		int p=0;
		FORP(i,n-k,n-1) y[p++]=i;
		FORP(i,0,n-1) if (sa[i]>=k) y[p++]=sa[i]-k;
		
		FORP(i,0,m) c[i]=0;
		FORP(i,0,n-1) c[x[y[i]]]++;
		FORP(i,1,m) c[i]+=c[i-1];
		FORM(i,n-1,0) sa[--c[x[y[i]]]]=y[i];

		swap(x,y);
		p=1; x[sa[0]]=0;
		FORP(i,1,n-1)
			if (y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k])
				x[sa[i]]=p-1;
			else x[sa[i]]=p++;
		if (p>=n) break;
		m=p;
	}
}
int main(){
	read(n); int tmp=n;
	int m=0;
	FORP(i,0,n-1) { read(s[i]); m=max(m,s[i]); s[n+i]=s[i];}
	s[n+n]=m+1;
	n=n+n+1;
	build_sa(m+2);
	for (int i=0;i<tmp;i++) printf("%d%c",s[(sa[0])%tmp+i],i==tmp-1?'
':' '); }

좋은 웹페이지 즐겨찾기