| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
PKU上自己做的第一道题2774##NOI2016##SCOI2016##NOIP2015
调了三个小时发现height求错了orz
每日打表
Bless All
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
int n,m,sa[300005],rank[300005],height[300005],wx[300005],wy[300005],wc[300005],r[300005];
int n1,n2;
string st,sr;
bool cmp(int *y,int a,int b,int c)
{
return (y[a]==y[b]&&y[a+c]==y[b+c]);
}
void make_suffix()
{
int *x=wx,*y=wy,*c=wc;
for(int i=0;i<n;i++)
c[x[i]=r[i]]++;
for(int i=1;i<=m;i++)
c[i]+=c[i-1];
for(int i=n-1;i>=0;i--)
sa[--c[x[i]]]=i;
int p=0;
for(int j=1;p<n;j*=2,m=p){
p=0;
for(int i=n-j;i<n;i++)
y[p++]=i;
for(int i=0;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
for(int i=0;i<=m;i++)
c[i]=0;
for(int i=0;i<n;i++)
c[x[y[i]]]++;
for(int i=1;i<=m;i++)
c[i]+=c[i-1];
for(int i=n-1;i>=0;i--)
sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1;x[sa[0]]=0;
for(int i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}
void make_height()
{
int i, j, k = 0;
for(i =1;i<n;i++)rank[sa[i]]= i;
for(i =0;i<n;height[rank[i++]]=k)
for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
}
void readdata()
{
freopen("loli.in","r",stdin);
freopen("loli.out","w",stdout);
cin>>st;
n1=st.length();
n=st.length()+1;
cin>>sr;
n2=sr.length();
for(int i=0;i<st.length();i++)
r[i]=st[i];
r[st.length()]=1;
for(int i=0;i<sr.length();i++)
r[i+n]=sr[i];
n+=sr.length();m=128;
}
bool can(int a,int b)
{
if(a>b)
return can(b,a);
if(a<n1&&b>n1)
return true;
return false;
}
void solve()
{
int minn=0;
for(int i=2;i<n;i++)
if(can(sa[i],sa[i-1])){
//printf("%d %d %d %d\n",sa[i-1],sa[i],i,height[i]);
minn=max(minn,height[i]);
}
printf("%d\n",minn);
}
int main()
{
readdata();
make_suffix();
make_height();
solve();
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator