zoukankan      html  css  js  c++  java
  • hdu5558 后缀数组

    Alice's Classified Message

    Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 312    Accepted Submission(s): 122


    Problem Description
    Alice wants to send a classified message to Bob. She tries to encrypt the message with her original encryption method. The message is a string S, which consists of N lowercase letters.

    S[ab] means a substring of S ranging from S[a] to S[b] (0ab<N). If the first i letters have been encrypted, Alice will try to find a magic string P. Assuming P has K letters, P is the longest string which satisfies P=S[T...T+K1] (0T<iT+KN) and P=S[ii+K1](i+KN). In other words, P is a substring of S, of which starting address is within [0...i1], and P is also a prefix of S[i...N1]. If P exists, Alice will append integer K and T to ciphertext. If T is not unique, Alice would select the minimal one. And then i is incremented by K. If P does not exist, Alice will append -1 and the ASCII code of letter S[i] to ciphertext, and then increment i by 1.

    Obviously the first letter cannot be encrypted. That is to say, P does not exist when i=0. So the first integer of ciphertext must be -1, and the second integer is the ASCII code of S[0].

    When i=N, all letters are encrypted, and Alice gets the final ciphertext, which consists of many pairs of integers. Please help Alice to implement this method.
     
    Input
    The first line of input contains an integer T, which represents the number of test cases (T50). Each test case contains a line of string, which has no more than 100000 lowercase letters. It is guaranteed that the total length of the strings is not greater than 2×106.
     
    Output
    For each test case, output a single line consisting of “Case #X:” first. X is the test case number starting from 1. Output the ciphertext in the following lines. Each line contains two integers separated by a single space.
     
    Sample Input
    2
    aaaaaa
    aaaaabbbbbaaabbc
     
    Sample Output
    Case #1:
    -1 97
    5 0
    Case #2:
    -1 97
    4 0
    -1 98
    4 5
    5 2
    -1 99
      1 /*
      2 hdu5558  后缀数组
      3 
      4 从[1,n]对于每个i,求suff[j](j < i)与suff[i]的最长公共前缀,
      5 如果有多个,取最小的那个
      6 
      7 我们可以通过后缀数组先求出,如果i-1和i,i和i+1都有公共前缀,
      8 那么i-1和i+1也有公共前缀,所以可以先处理出每个i的左右界限。然后对于i左右扫描一下即可
      9 
     10 然后枚举i,从pre[i]-nex[i]找到合适的结果即可
     11 
     12 hhh-2016-03-10 18:17:04
     13 */
     14 #include <algorithm>
     15 #include <cmath>
     16 #include <queue>
     17 #include <iostream>
     18 #include <cstring>
     19 #include <map>
     20 #include <cstdio>
     21 #include <vector>
     22 #include <functional>
     23 #define lson (i<<1)
     24 #define rson ((i<<1)|1)
     25 using namespace std;
     26 typedef long long ll;
     27 const int maxn = 150050;
     28 
     29 int t1[maxn],t2[maxn],c[maxn];
     30 bool cmp(int *r,int a,int b,int l)
     31 {
     32     return r[a]==r[b] &&r[l+a] == r[l+b];
     33 }
     34 
     35 void get_sa(int str[],int sa[],int Rank[],int height[],int n,int m)
     36 {
     37     n++;
     38     int p,*x=t1,*y=t2;
     39     for(int i = 0; i < m; i++) c[i] = 0;
     40     for(int i = 0; i < n; i++) c[x[i] = str[i]]++;
     41     for(int i = 1; i < m; i++) c[i] += c[i-1];
     42     for(int i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
     43     for(int j = 1; j <= n; j <<= 1)
     44     {
     45         p = 0;
     46         for(int i = n-j; i < n; i++) y[p++] = i;
     47         for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j;
     48         for(int i = 0; i < m; i++) c[i] = 0;
     49         for(int i = 0; i < n; i++) c[x[y[i]]]++ ;
     50         for(int i = 1; i < m; i++) c[i] += c[i-1];
     51         for(int i = n-1; i >= 0; i--)  sa[--c[x[y[i]]]] = y[i];
     52 
     53         swap(x,y);
     54         p = 1;
     55         x[sa[0]] = 0;
     56         for(int i = 1; i < n; i++)
     57             x[sa[i]] = cmp(y,sa[i-1],sa[i],j)? p-1:p++;
     58         if(p >= n) break;
     59         m = p;
     60     }
     61     int k = 0;
     62     n--;
     63     for(int i = 0; i <= n; i++)
     64         Rank[sa[i]] = i;
     65     for(int i = 0; i < n; i++)
     66     {
     67         if(k) k--;
     68         int j = sa[Rank[i]-1];
     69         while(str[i+k] == str[j+k]) k++;
     70         height[Rank[i]] = k;
     71     }
     72 }
     73 
     74 int pre[maxn],nex[maxn];
     75 int Rank[maxn],height[maxn];
     76 int sa[maxn],str[maxn];
     77 char a[maxn];
     78 int len;
     79 
     80 int main()
     81 {
     82     int T,cas = 1;
     83     scanf("%d",&T);
     84     while(T--)
     85     {
     86         scanf("%s",a);
     87         int len = 0;
     88         for(int i =0;a[i] != ''; i++)
     89         {
     90             str[len++] = a[i]-'a'+1;
     91         }
     92         str[len] = 0;
     93         get_sa(str,sa,Rank,height,len,30);
     94 
     95         for(int i = 1; i <= len; i++)
     96         {
     97             if(height[i] == 0)
     98                 pre[i] = i;
     99             else
    100                 pre[i] = pre[i-1];
    101         }
    102 
    103         for(int i = len; i >= 1; i--)
    104         {
    105             if(height[i+1] == 0 || i == len) nex[i] = i;
    106             else nex[i] = nex[i+1];
    107         }
    108 
    109         int i = 0;
    110          printf("Case #%d:
    ",cas++);
    111         while(i < len)
    112         {
    113             int now = Rank[i];   //i的排名
    114             int k = 0,t = i;
    115             int mi = height[now];
    116             for(int j = now-1; j >= pre[now]; j--)
    117             {
    118                 mi = min(mi,height[j+1]);
    119                 if(mi < k)
    120                     break;
    121                 if(sa[j] < i)
    122                 {
    123                     if(mi > k || (mi==k && sa[j] < t))
    124                     {
    125                         k = mi;
    126                         t = sa[j];
    127                     }
    128                 }
    129             }
    130             if(now+1 <= nex[now]) mi = height[now+1];
    131             for(int j = now+1; j <= nex[now]; j++)
    132             {
    133                 mi = min(mi,height[j]);
    134                 if(mi < k)
    135                     break;
    136                 if(sa[j] < i)
    137                 {
    138                     if(mi > k || (mi==k && sa[j] < t))
    139                     {
    140                         t = sa[j];
    141                         k = mi;
    142                     }
    143                 }
    144             }
    145 
    146             if(k == 0) printf("-1 %d
    ",a[i]);
    147             else printf("%d %d
    ",k,t);
    148             if(k) i+=k;
    149             else i++;
    150         }
    151     }
    152     return 0;
    153 }


    
    
  • 相关阅读:
    Linux开机启动和用户登录启动配置 .
    配置ssh信任
    .bash_profile和.bashrc的区别(如何设置生效)
    HttpUrlConnection底层实现和关于java host绑定ip即时生效的设置及分析
    一个shell写的urldecode代码
    用 DomIt! 做简单的 XML 处理工作
    windows主机安全设置 PHP5研究室
    介绍几个测试虚拟主机性能的工具 测速(全球范围),测同服网站,测路由
    Dreamhost开启allow_url_fopen采集功能教程
    配置Editplus的PHP调试环境
  • 原文地址:https://www.cnblogs.com/Przz/p/5409569.html
Copyright © 2011-2022 走看看