题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4691
题意:给出Input,求出Compressed output。输出各用多少字节。
思路:求后缀数组。每次直接询问两个后缀的最长公共前缀。
int r[N],sa[N],wa[N],wb[N],wd[N],rank[N],h[N];
int cmp(int *r,int a,int b,int len)
{
return r[a]==r[b]&&r[a+len]==r[b+len];
}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
FOR0(i,m) wd[i]=0;
FOR0(i,n) wd[x[i]=r[i]]++;
FOR1(i,m-1) wd[i]+=wd[i-1];
FORL0(i,n-1) sa[--wd[x[i]]]=i;
for(j=1,p=1;p<n;j<<=1,m=p)
{
p=0;
FOR(i,n-j,n-1) y[p++]=i;
FOR0(i,n) if(sa[i]>=j) y[p++]=sa[i]-j;
FOR0(i,m) wd[i]=0;
FOR0(i,n) wd[x[i]]++;
FOR1(i,m-1) wd[i]+=wd[i-1];
FORL0(i,n-1) sa[--wd[x[y[i]]]]=y[i];
t=x;x=y;y=t;p=1;x[sa[0]]=0;
FOR1(i,n-1) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}
void calHeight(int *r,int *sa,int n)
{
int i,j,k=0;
FOR1(i,n) rank[sa[i]]=i;
FOR0(i,n)
{
if(k) k--;
j=sa[rank[i]-1];
while(i+k<n&&j+k<n&&r[i+k]==r[j+k]) k++;
h[rank[i]]=k;
}
}
int f[N][20],n;
void init()
{
int i,j;
FOR1(i,n) f[i][0]=h[i];
for(i=1;1+(1<<i)<=n;i++) for(j=1;j+(1<<i)-1<=n;j++)
{
f[j][i]=min(f[j][i-1],f[j+(1<<(i-1))][i-1]);
}
}
int cal(int a,int b)
{
if(a==b) return n-a;
a=rank[a];
b=rank[b];
if(a>b) swap(a,b);
a++;
int m=floor(log(1.0*(b-a+1))/log(2.0));
return min(f[a][m],f[b-(1<<m)+1][m]);
}
char s[N];
int getBit(int x)
{
if(x<10) return 1;
int ans=0;
while(x) ans++,x/=10;
return ans;
}
void deal()
{
i64 ans1=0,ans2=0;
int preL,preR,L,R;
int m,k;
RD(m);
RD(preL,preR);
ans1=preR-preL+1;
ans2=preR-preL+3;
m--;
while(m--)
{
RD(L,R);
k=min(min(R-L,preR-preL),cal(L,preL));
ans1+=R-L+1;
ans2+=getBit(k)+1+R-L-k+1;
preL=L;
preR=R;
}
printf("%I64d %I64d\n",ans1,ans2);
}
int main()
{
while(scanf("%s",s)!=-1)
{
n=strlen(s);
int i;
FOR0(i,n) r[i]=s[i];
r[n]=0;
da(r,sa,n+1,130);
calHeight(r,sa,n);
init();
deal();
}
}