Front compression(后缀数组+rmq)

Front compression

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 2109    Accepted Submission(s): 731


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
  
  
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
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   5932  5931  5930  5928  5926 


后缀数组应用之一:熟悉掌握后缀数组中各个数组的意义。对于求区间最小,我们用rmq算法即可。存个模板吧。




#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
const int maxn = 100100;
#define N 100100 
int maxl[N][30], minl[N][30];  
int _rank[maxn],wb[maxn],wv[maxn],wss[maxn];
int height[maxn]; 
int n;
void S_table()  
{  
    int l = int(log((double)n)/log(2.0));  
    for (int j=1;j<=l;j++)  
    {  
        for (int i=1; i + (1 << (j-1) ) - 1 <=n;++i)  
        {  
            maxl[i][j] = max(maxl[i][j-1], maxl[i + (1 << (j-1) )][j-1]);  
            minl[i][j] = min(minl[i][j-1], minl[i + (1 << (j-1) )][j-1]);  
        }  
    }  
}   
int rmq(int l, int r)  
{  
    int k = int(log((double)(r-l+1))/log(2.0));  
    int a1 = max(maxl[l][k], maxl[r - (1<<k) + 1][k]);  
    int a2 = min(minl[l][k], minl[r - (1<<k) + 1][k]);  
   // printf("Max: %d Min: %d\n", a1, a2);  
	return a2;
}   
void calheight(int *r,int *sa,int n)  
{  
	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++);  
	 return;  
}  
bool cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b] && r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=_rank,*y=wb,*t;
    for(i=0;i<m;i++) wss[i]=0;
    for(i=0;i<n;i++) wss[x[i]=r[i]]++;
    for(i=1;i<m;i++) wss[i]+=wss[i-1];
    for(i=n-1;i>=0;i--)
       sa[--wss[x[i]]]=i;

    for(j=1,p=1;p<n;j*=2,m=p)
    {
        for(p=0,i=n-j;i<n;i++) y[p++]=i;
        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
        for(i=0;i<n;i++) wv[i]=x[y[i]];
        for(i=0;i<m;i++) wss[i]=0;
        for(i=0;i<n;i++) wss[wv[i]]++;
        for(i=1;i<m;i++) wss[i]+=wss[i-1];
        for(i=n-1;i>=0;i--) sa[--wss[wv[i]]]=y[i];
        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
        x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
    return;
}
long long wei(int s)
{
	long long ans=0;
	if(s==0)
		ans++;
	int n=s;
	while(n>0)
	{
		n=n/10;
		ans++;
	}
	return ans;
}
char s[maxn];
int l;
int r[maxn],sa[maxn];
int main()
{

    while(cin>>s)
    {
	    l = strlen(s);   l++;
        for(int i=0; i<l-1; i++) r[i] = s[i]-'a'+1;
        r[l-1] = 0;
        da(r,sa,l,27);
		calheight(r,sa,l-1); 
	/*	  for(i=0; i<l-1; i++)  // rank[i] : suffix(i)排第几
           printf("rank[%d] =  %d\n",i,_rank[i]);
        printf("\n");
        for(i=0; i<l; i++)   // sa[i] : 排在第i个的是谁
           printf("sa[%d] =  %d\n",i,sa[i]);
		system("pause");*/
		for(int i=1;i<l;i++)
		{
			maxl[i][0]=minl[i][0]=height[i];
		}
		n=l-1;
		S_table();
		int query;
		scanf("%d",&query);
		int left,right;
		scanf("%d%d",&left,&right);
		long long ans=2+(right-left)+1;
		long long ans1=right-left;
		//cout<<ans<<endl;
		int max_=right-left;
		int pre=left;
		for(int i=1;i<query;i++)
		{
			scanf("%d%d",&left,&right);
			ans1=ans1+(right-left);
			max_=min(max_,right-left);
			int nowsuff=left;
			int rr=max(_rank[pre],_rank[nowsuff]);
			int ll=min(_rank[pre],_rank[nowsuff]);
			int aa;
			if(pre==nowsuff)
			{
				aa=max_;
			}
			else
				aa=rmq(ll+1,rr);
			//cout<<height[3]<<' '<<height[4]<<endl;
			if(aa>max_)
				aa=max_;
			//cout<<aa<<"yws"<<endl;
			int add=wei(aa);
			ans=ans+add+1+(right-left-aa)+1;
			max_=right-left;
			pre=left;
			//cout<<ans<<endl;
		}
       cout<<(long long)ans1+query<<" "<<ans<<endl;	   
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值