zoukankan      html  css  js  c++  java
  • HDU 4691 Front compression

    Front compression

    Time Limit: 5000ms
    Memory Limit: 102400KB
    This problem will be judged on HDU. Original ID: 4691
    64-bit integer IO format: %I64d      Java class name: Main
     
    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

    Source

     
    解题:后缀数组配合RMQ的使用
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <climits>
      7 #include <vector>
      8 #include <queue>
      9 #include <cstdlib>
     10 #include <string>
     11 #include <set>
     12 #include <stack>
     13 #define LL long long
     14 #define pii pair<int,int>
     15 #define INF 0x3f3f3f3f
     16 using namespace std;
     17 const int maxn = 100010;
     18 int rk[maxn],wb[maxn],wv[maxn],wd[maxn],lcp[maxn];
     19 bool cmp(int *r,int i,int j,int k){
     20     return r[i] == r[j] && r[i+k] == r[j+k];
     21 }
     22 void da(int *r,int *sa,int n,int m){
     23     int i,k,p,*x = rk,*y = wb;
     24     for(i = 0; i < m; ++i) wd[i] = 0;
     25     for(i = 0; i < n; ++i) wd[x[i] = r[i]]++;
     26     for(i = 1; i < m; ++i) wd[i] += wd[i-1];
     27     for(i = n-1; i >= 0; --i) sa[--wd[x[i]]] = i;
     28 
     29     for(p = k = 1; p < n; k <<= 1,m = p){
     30         for(p = 0,i = n-k; i < n; ++i) y[p++] = i;
     31         for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;
     32         for(i = 0; i < n; ++i) wv[i] = x[y[i]];
     33 
     34         for(i = 0; i < m; ++i) wd[i] = 0;
     35         for(i = 0; i < n; ++i) wd[wv[i]]++;
     36         for(i = 1; i < m; ++i) wd[i] += wd[i-1];
     37         for(i = n-1; i >= 0; --i) sa[--wd[wv[i]]] = y[i];
     38 
     39         swap(x,y);
     40         x[sa[0]] = 0;
     41         for(p = i = 1; i < n; ++i)
     42             x[sa[i]] = cmp(y,sa[i-1],sa[i],k)?p-1:p++;
     43     }
     44 }
     45 void calcp(int *r,int *sa,int n){
     46     for(int i = 1; i <= n; ++i) rk[sa[i]] = i;
     47     int h = 0;
     48     for(int i = 0; i < n; ++i){
     49         if(h > 0) h--;
     50         for(int j = sa[rk[i]-1]; i+h < n && j+h < n; h++)
     51             if(r[i+h] != r[j+h]) break;
     52         lcp[rk[i]] = h;
     53     }
     54 }
     55 int st[maxn][20];
     56 void init(int n){
     57     memset(st,0,sizeof(st));
     58     for(int i = 1; i <= n; ++i) st[i][0] = lcp[i];
     59     for(int i = 1; 1<<i <= n; ++i){
     60         for(int j = 1; j+(1<<i) <= n+1; ++j)
     61             st[j][i] = min(st[j][i-1],st[j+(1<<(i-1))][i-1]);
     62     }
     63 }
     64 int query(int s,int t){
     65     s = rk[s];
     66     t = rk[t];
     67     if(s > t) swap(s,t);
     68     s++;
     69     int k = log2(t - s + 1);
     70     return min(st[s][k],st[t-(1<<k)+1][k]);
     71 }
     72 int r[maxn],sa[maxn];
     73 char str[maxn];
     74 int mb(int x){
     75     int ans = 0;
     76     if(x == 0) return 1;
     77     while(x){
     78         x /= 10;
     79         ++ans;
     80     }
     81     return ans;
     82 }
     83 int main() {
     84     int hn,x,y;
     85     while(~scanf("%s",str)){
     86         int len = strlen(str);
     87         for(int i = 0; str[i]; ++i)
     88             r[i] = str[i];
     89         r[len] = 0;
     90         da(r,sa,len+1,128);
     91         calcp(r,sa,len);
     92         init(len);
     93         LL ans = 0,ans2 = 0;
     94         int px,py;
     95         scanf("%d",&hn);
     96         for(int i = 0; i < hn; ++i){
     97             scanf("%d %d",&x,&y);
     98             ans += y -  x + 1;
     99             if(i == 0) ans2 += y - x + 3;
    100             else{
    101                 int mlen = min(y-x,py-px);
    102                 if(px == x){
    103                     if(mlen == y - x) ans2 += mb(y-x) + 2;
    104                     else ans2 += mb(mlen) + 2 + y - x - mlen;
    105                 }else{
    106                     int com = query(px,x);
    107                     if(com >= mlen){
    108                         com = mlen;
    109                         if(com == y - x) ans2 += mb(com) + 2;
    110                         else ans2 += mb(com) + y - x - com + 2;
    111                     }else if(com) ans2 += mb(com) + 2 + y - x - com;
    112                     else ans2 += y - x + 3;
    113                 }
    114             }
    115             px = x;
    116             py = y;
    117         }
    118         cout<<ans<<" "<<ans2<<endl;
    119     }
    120     return 0;
    121 }
    View Code
  • 相关阅读:
    深入浅出单实例Singleton设计模式
    设计模式之监听器模式
    Android 驱动(二) IIC简单介绍
    用XCA(X Certificate and key management)可视化程序管理SSL 证书(3)--创建自己定义的凭证管理中心(Certificate Authority)
    编译android-4.3.1_r源代码并刷到自己的Galaxy Nexus I9250真机上
    C++中的单例模式
    MessageDigest简单介绍
    白话经典算法系列之中的一个 冒泡排序的三种实现
    KNN算法理解
    TCP/IP数据包结构具体解释
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4074628.html
Copyright © 2011-2022 走看看