Hdoj topic 4691 Front compression (suffix array +rmq longest prefix)

Source: Internet
Author: User

Front compressionTime limit:5000/5000 MS (java/others) Memory limit:102400/102400 K (java/others)
Total submission (s): 1652 Accepted Submission (s): 604


Problem Descriptionfront compression is a type of delta encoding compression algorithm whereby common prefixes and their l Engths is recorded so, they need not be duplicated. For example:

The size of the input is a bytes, while the size of the compressed output is +. Here, every space and newline are also counted as 1 byte.
Given the input, each line of which was a substring of a long string, what's sizes of it and corresponding compressed out Put?
Inputthere is multiple test cases. Process to the End of File.
The first line of all test case was 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 and integers 0≤a < B≤length (S), indicating that's line of the input is Su bstring [A, B) of S.
Outputfor each test case, output the sizes of the input and corresponding compressed output.
Sample Input
Frcode20 6unitedstatesofamerica30 21myxophytamyxopodnabnabbednabbingnabit60 99 1616 1919 2525 3232 37

Sample Output
14 1242 3143 40

Authorzejun Wu (Watashi)
Source2013 multi-university Training Contest 9
Recommendzhuyuanchen520 | We have carefully selected several similar problems for you:5421 5420 5419 5418 5417 Read the question, that is, it and its last prefix minus, in addition to this number plus Plus a carriage return AC code
#include <stdio.h> #include <string.h> #include <algorithm> #include <io          stream> #define MIN (A, b) (A&GT;B?B:A) #define MAX (A, B) (A&GT;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]]&AMP;&AMP;SA[I-1]+LEN&LT;N&AMP;&AMP;SA[I]+LEN&L                  T;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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj topic 4691 Front compression (suffix array +rmq longest prefix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.