Front compression
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 1386 Accepted Submission(s): 519
Problem Description
Front compression is a type of delta encoding compression algorithm whereby common prefixes and their lengths are recorded so that they need not be duplicated. For example:
The size of the input is 43 bytes, while the size of the compressed output is 40. Here, every space and newline is also counted as 1 byte.
Given the input, each line of which is a substring of a long string, what are sizes of it and corresponding compressed output?
![](https://i-blog.csdnimg.cn/blog_migrate/fe5b05bd3f66d649335474cddbc5e96b.jpeg)
The size of the input is 43 bytes, while the size of the compressed output is 40. Here, every space and newline is also counted as 1 byte.
Given the input, each line of which is a substring of a long string, what are sizes of it and corresponding compressed output?
Input
There are multiple test cases. Process to the End of File.
The first line of each test case is a long string S made up of lowercase letters, whose length doesn't exceed 100,000. The second line contains a integer 1 ≤ N ≤ 100,000, which is the number of lines in the input. Each of the following N lines contains two integers 0 ≤ A < B ≤ length(S), indicating that that line of the input is substring [A, B) of S.
The first line of each test case is a long string S made up of lowercase letters, whose length doesn't exceed 100,000. The second line contains a integer 1 ≤ N ≤ 100,000, which is the number of lines in the input. Each of the following N lines contains two integers 0 ≤ A < B ≤ length(S), indicating that that line of the input is substring [A, B) of S.
Output
For each test case, output the sizes of the input and corresponding compressed output.
Sample Input
frcode 2 0 6 0 6 unitedstatesofamerica 3 0 6 0 12 0 21 myxophytamyxopodnabnabbednabbingnabit 6 0 9 9 16 16 19 19 25 25 32 32 37
Sample Output
14 12 42 31 43 40
Author
Zejun Wu (watashi)
Source
求当前串和上一个串的公共前缀,算size的时候,记得加上回车,算corresponding compressed output时,数字,空格,剩余的字符,回车,都得加上,剩余字符是0的话,特殊处理一下。注意得把公共前缀的长度当成字符来考虑,最终结果用__int64。
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string.h>
#define N 100005
using namespace std;
int t1[N],t2[N],c[N],s[N],rank[N],height[N],sa[N],d[N][30],a[N],b[N];
char str[N];
void build_sa(int *s,int n,int m)
{
int *x=t1,*y=t2,p,i,k;
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[i]=s[i]]++;
for(i=1;i<m;i++) c[i]+=c[i-1];
for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
for(k=1;k<=n;k<<=1)
{
p=0;
for(i=n-k;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[y[i]]]++;
for(i=1;i<m;i++) c[i]+=c[i-1];
for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y); p=1;
x[sa[0]]=0;
for(i=1;i<n;i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]? p-1 : p++;
if(p>=n)
break;
m=p;
}
}
void getheight(int n)
{
int i,j,k=0;
for(i=0;i<=n;i++)
rank[sa[i]]=i;
for(i=0;i<n;i++)
{
if(k) k--;
j=sa[rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
void rmqinit(int n)
{
int i,j;
memset(d,0,sizeof(d));
for(i=1;i<=n;i++)
d[i][0]=height[i];
for(j=1;(1<<j)<=n;j++)
for(i=1;i+(1<<j)-1<=n;i++)
d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
int rmq(int l,int r)
{
int k=0;
while((1<<(k+1))<=r-l+1) k++;
return min(d[l][k],d[r-(1<<k)+1][k]);
}
int lcp(int a,int b)
{
a=rank[a]; b=rank[b];
if(a>b) swap(a,b);
return rmq(a+1,b);
}
int main()
{
int n,m,i,p,num;
__int64 sum,ans;
while(scanf("%s",str)!=EOF)
{
n=strlen(str);
for(i=0;str[i]!='\0';i++)
s[i]=str[i];
s[n]=0;
build_sa(s,n+1,130);
getheight(n);
rmqinit(n);
scanf("%d",&n);
ans=sum=0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&a[i],&b[i]);
sum+=b[i]-a[i]+1;
if(i==1)
{
ans+=b[i]-a[i]+3;
continue;
}
if(a[i]==a[i-1])
p=b[i]-a[i];
else
p=lcp(a[i],a[i-1]);
p=min(p,b[i-1]-a[i-1]);
p=min(p,b[i]-a[i]);
m=b[i]-a[i]-p;
if(p==0)
num=2;
else
num=(int)log10(p*1.0)+2;
ans=ans+num+m+1;
}
printf("%I64d %I64d\n",sum,ans);
}
return 0;
}