From Evernote: |
uva 760 |
後綴數組練習
[sourcecode language="cpp"]
//============================================================================
// Name : DNA Sequencing.cpp
// Date : 2013 2013/2/16 下午10:28:08
// 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 suffixarray{
static const int maxn=1005;
int c[maxn],ra[maxn],sa[maxn],tra[maxn],tsa[maxn];
int rank[maxn],height[maxn];
int n;
char str[maxn];
void sort(int k){
Set(c,0);
int sum=0;
for(int i=k;i<n;i++)c[ra[i]]++;
for(int i=0;i<max(128,n);i++){
int tmp=c[i];
c[i]=sum;
sum+=tmp;
}
int p=0;
for(int i=0;i<n;i++)
if(sa[i]>=n-k)tsa[p++]=sa[i];
for(int i=0;i<n;i++){
if(sa[i]<n-k)tsa[p+c[ra[sa[i]+k]]++]=sa[i];
}
// for(int i=0;i<n;i++){
// printf("%d ",tsa[i]);
// }
// Pln();
memcpy(sa,tsa,sizeof(sa));
}
void build(){
n=strlen(str);
for(int i=0;i<=n;i++){
ra[i]=str[i];
sa[i]=i;
}
for(int k=1;k<n;k<<=1){
sort(k);
sort(0);
int r=tra[sa[0]]=0;
for(int i=1;i<n;i++){
int s1=sa[i];
int s2=sa[i-1];
bool b=true;
b&=ra[s1]==ra[s2];
b&=ra[s1+k]==ra[s2+k];
tra[sa[i]]=b?r:++r;
}
memcpy(ra,tra,sizeof(ra));
}
// printf("<<sa ");
// for(int i=0;i<n;i++){
// printf("%d ",sa[i]);
// }
// Pln();
}
void build_h(){
str[n]='&';
for(int i=0;i<n;i++)
rank[sa[i]]=i;
for(int i=0,k=0;i<n;i++){
if(k)k--;
int prerank=rank[i]-1;
if(prerank==-1)continue;
for(int j=sa[rank[i]-1];str[i+k]==str[j+k];k++);
height[rank[i]]=k;
}
// for(int i=0;i<n;i++)printf("%d ",height[i]);Pln();
}
};
char str[1005];
int main() {
ios_base::sync_with_stdio(0);
int ff=0;
while(gets(str)){
suffixarray in;
int len=strlen(str);
str[len]='^';
gets(str+len+1);
getchar();
// printf("%s\n",str);
strcpy(in.str,str);
in.build();
in.build_h();
int mxx=0;
for(int i=1;i<in.n;i++)
if((in.sa[i-1]<len&&in.sa[i]>len)||(in.sa[i-1]>len&&in.sa[i]<len))mxx=max(mxx,in.height[i]);
// debug("%d\n",mxx);
if(ff++)Pln();
if(mxx==0)printf("No common sequence.\n");
else{
for(int i=1;i<in.n;i++){
if(in.height[i]==mxx&&((in.sa[i-1]<len&&in.sa[i]>len)||(in.sa[i-1]>len&&in.sa[i]<len))){
for(int j=in.sa[i];j<in.sa[i]+mxx;j++)putchar(in.str[j]);
putchar('\n');
for(int j=i;j<in.n&&in.height[j]>=mxx;j++)i=j;
}
}
}
}
}
[/sourcecode]
留言
張貼留言