![]() | ||||||||||
| ||||||||||
Front compressionTime Limit: 5000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 1271 Accepted Submission(s): 473
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?
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.
Output
For each test case, output the sizes of the input and corresponding compressed output.
Sample Input
Sample Output
|
思路:求一下后缀数组就完了,要注意空格和回车的个数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
typedef long long LL;
int sa[maxn],height[maxn],rank[maxn],t[maxn],t2[maxn],c[maxn];
int n,d[maxn][25],N,L[maxn],R[maxn];
char str[maxn];
void build_sa(int m,int n)
{
int *x=t,*y=t2;
for(int i=0;i<m;i++)c[i]=0;
for(int i=0;i<n;i++)c[x[i]=str[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;
for(int k=1;k<=n;k<<=1)
{
int p=0;
for(int i=n-k;i<n;i++)y[p++]=i;
for(int i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
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);
x[sa[0]]=0;p=1;
for(int 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 k=0;
for(int i=1;i<=n;i++)rank[sa[i]]=i;
for(int i=0;i<n;i++)
{
if(k)k--;
int j=sa[rank[i]-1];
while(str[i+k]==str[j+k])k++;
height[rank[i]]=k;
}
}
void initRMQ()
{
for(int i=0;i<=n;i++)d[i][0]=height[i];
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<(j-1))<=n;i++)//这里要从1到n,因为height数组是从1到n
d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
int LCP(int a,int b)
{
if(a==b)return n;
int x=rank[a],y=rank[b];
if(x>y)swap(x,y);
x++;
int k=0;
while((1<<(k+1))<=(y-x+1))k++;
return min(d[x][k],d[y-(1<<k)+1][k]);
}
int count(int x)
{
int num=0;
if(x==0)return 1;
while(x)
{
num++;
x/=10;
}
return num;
}
void solve()
{
scanf("%d",&N);
LL ans1=0,ans2=0;
for(int i=1;i<=N;i++)
{
scanf("%d%d",&L[i],&R[i]);
ans1=ans1+R[i]-L[i]+1;
if(i==1){ans2=ans2+3+R[i]-L[i];continue;}
int len=min(min(R[i]-L[i],R[i-1]-L[i-1]),LCP(L[i],L[i-1]));
ans2=ans2+count(len)+2+R[i]-L[i]-len;
}
cout<<ans1<<" "<<ans2<<endl;
}
int main()
{
while(scanf("%s",str)!=EOF)
{
n=strlen(str);
build_sa(123,n+1);
memset(rank,0,sizeof(rank));
getheight(n);
initRMQ();
solve();
}
return 0;
}