后缀数组hdu4961

前端压缩算法解析

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
Author ID 
Password 
  Register new ID

Front compression

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

思路:求一下后缀数组就完了,要注意空格和回车的个数

#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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值