跳到主要內容

uva 11512











From Evernote:



uva 11512



這題是後綴數組經典題

後綴數組雖然好理解,不過代碼真的太過深澳,需要多練練,此題也是參考很多網路上的解題報告。

[sourcecode language="cpp"]
//============================================================================
// Name : GATTACA.cpp
// Date : 2013 2013/2/1 下午6:06:22
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 100005
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
struct suffix_array{
static const int maxn=1005;
int n,wv[maxn],wa[maxn],ws[maxn],wb[maxn],sa[maxn];
int rank[maxn],height[maxn];
char str[maxn];
int cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void build(){
int m=128;
int *x=wa,*y=wb,*t;
n=strlen(str);
str[n++]=0;
for(int i=0;i<m;i++)ws[i]=0;
for(int i=0;i<n;i++)ws[x[i]=str[i]]++;
for(int i=1;i<m;i++)ws[i]+=ws[i-1];
for(int i=n-1;i>=0;i--)sa[--ws[x[i]]]=i;
for(int i=0,j=1,p=1;p<n;j*=2,m=p){
// for(i=0;i<n;i++)printf("%d",sa[i]);Pln();
// printf("j%d\n",j);
for(p=0,i=n-j;i<n;i++)y[p++]=i;
for(i=0;i<n;i++)if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0;i<n;i++)wv[i]=x[y[i]];
for(i=0;i<m;i++)ws[i]=0;
for(i=0;i<n;i++)ws[wv[i]]++;
for(i=1;i<m;i++)ws[i]+=ws[i-1];
for(i=n-1;i>=0;i--)sa[--ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++){
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}
}
void buildh(){
n--;
int j=0,k=0;
for(int i=1;i<=n;i++)rank[sa[i]]=i;
// for(int i=0;i<n;i++){
//// printf("%d ",rank[i]);
// }Pln();
for(int i=0;i<n;height[rank[i++]]=k)
for(k?k--:0,j=sa[rank[i]-1];str[i+k]==str[j+k];k++);
}
};
int n;
int main() {
ios_base::sync_with_stdio(0);

while(~scanf("%d",&n)&&n){
while(n--){
suffix_array in;
scanf("%s",in.str);
in.build();
in.buildh();
int ans=0;
for(int i=1;i<=in.n;i++){
ans=max(ans,in.height[i]);
}
if(!ans)printf("No repetitions found!\n");
else{
for(int i=1;i<=in.n;i++){
if(in.height[i]==ans){
int st=in.sa[i],cnt=0;
for(int j=0;j<ans;j++)printf("%c",in.str[st+j]);
printf(" ");
for(int j=i;j<=in.n&&in.height[j]==ans;j++)cnt++;
printf("%d\n",cnt+1);
break;
}
}
}
}
}

}

[/sourcecode]

留言