码迷,mamicode.com
首页 > 编程语言 > 详细

HDOJ 题目4691 Front compression(后缀数组+RMQ最长前缀)

时间:2015-08-29 14:06:05      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

Front compression

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


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:  5421 5420 5419 5418 5417 
 
看懂题就会了,,就是把它和它上一个的最长前缀减去,在加上这个数字再加上空格再加上一个回车
ac代码
技术分享
#include<stdio.h>            
#include<string.h>            
#include<algorithm>            
#include<iostream>           
#define min(a,b) (a>b?b:a)        
#define max(a,b) (a>b?a:b)         
using namespace std;          
char str[103030];        
int sa[103030],Rank[103030],rank2[103030],height[103030],c[103030],*x,*y,len;      
int n;      
void cmp(int n,int sz)      
{      
    int i;      
    memset(c,0,sizeof(c));      
    for(i=0;i<n;i++)      
        c[x[y[i]]]++;      
    for(i=1;i<sz;i++)      
        c[i]+=c[i-1];      
    for(i=n-1;i>=0;i--)      
        sa[--c[x[y[i]]]]=y[i];      
}      
void build_sa(char *s,int n,int sz)      
{      
    x=Rank,y=rank2;      
    int i,j;      
    for(i=0;i<n;i++)      
        x[i]=s[i],y[i]=i;      
    cmp(n,sz);      
    int len;      
    for(len=1;len<n;len<<=1)      
    {      
        int yid=0;      
        for(i=n-len;i<n;i++)      
        {      
            y[yid++]=i;      
        }      
        for(i=0;i<n;i++)      
            if(sa[i]>=len)      
                y[yid++]=sa[i]-len;      
            cmp(n,sz);      
        swap(x,y);      
        x[sa[0]]=yid=0;      
        for(i=1;i<n;i++)      
        {      
            if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len])      
                x[sa[i]]=yid;      
            else      
                x[sa[i]]=++yid;      
        }      
        sz=yid+1;      
        if(sz>=n)      
            break;      
    }      
    for(i=0;i<n;i++)      
        Rank[i]=x[i];      
}      
void getHeight(char *s,int n)      
{      
    int k=0;      
    for(int i=0;i<n;i++)      
    {      
        if(Rank[i]==0)      
            continue;      
        k=max(0,k-1);      
        int j=sa[Rank[i]-1];      
        while(s[i+k]==s[j+k])      
            k++;      
        height[Rank[i]]=k;      
    }      
}    
int minv[103010][20],lg[103030];      
void init_lg()    
{    
    int i;    
    lg[1]=0;    
    for(i=2;i<102020;i++)    
    {    
        lg[i]=lg[i>>1]+1;    
    }    
}    
void init_RMQ(int n)    
{    
    int i,j,k;    
    for(i=1;i<=n;i++)    
    {    
        minv[i][0]=height[i];    
    }    
    for(j=1;j<=lg[n];j++)      
    {      
        for(k=0;k+(1<<j)-1<=n;k++)      
        {      
            minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);       
        }      
    }    
}    
int lcp(int l,int r)    
{    
	if(l==r)
	{
		return len-l;
	}
    l=Rank[l];    
    r=Rank[r];    
    if(l>r)    
        swap(l,r);    
    l++;    
    int k=lg[r-l+1];    
    return min(minv[l][k],minv[r-(1<<k)+1][k]);      
}   
__int64 fun(__int64 x)
{
	__int64 ans=0;
	if(x==0)
		return 1;
	while(x)
	{
		x/=10;
		ans++;
	}
	return ans;
}
int main()
{
	while(scanf("%s",str)!=EOF)
	{
		int t;
		len=strlen(str);
		build_sa(str,len+1,256);
		getHeight(str,len);
		scanf("%d",&t);
		init_lg();
		init_RMQ(len);
		__int64 ans1,ans2;
		ans1=ans2=0;
		int l,r;
		scanf("%d%d",&l,&r);
		ans1+=(r-l)+1;
		t--;
		ans2+=(r-l)+3;
		while(t--)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			ans1+=(b-a)+1;
			__int64 p;
			p=min(lcp(l,a),min((b-a),(r-l)));
			ans2+=(b-a)-p+2+fun(p);
			l=a;
			r=b;
		}
		printf("%I64d %I64d\n",ans1,ans2);
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

HDOJ 题目4691 Front compression(后缀数组+RMQ最长前缀)

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/48086521

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!