zoukankan      html  css  js  c++  java
  • hdu5558 Alice's Classified Message

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5558

    题目:

    Alice's Classified Message

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


    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 Nlowercase 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
     
    Source
     
    Recommend
    wange2014
     
    思路:
      建立sam,然后每次从root开始跑。
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 struct SAM
     4 {
     5     static const int MAXN = 100002<<1;//大小为字符串长度两倍
     6     static const int LetterSize = 26;
     7 
     8     int tot, last, ch[MAXN][LetterSize], fa[MAXN], len[MAXN];
     9     int sum[MAXN], tp[MAXN], cnt[MAXN]; //sum,tp用于拓扑排序,tp为排序后的数组
    10     int ft[MAXN];
    11     void init( void)
    12     {
    13         last = tot = 1;
    14         len[1] = 0;
    15         memset( ch[1], 0, sizeof ch[1]);
    16     }
    17 
    18     void add( int x, int pos)
    19     {
    20         int p = last, np = last = ++tot;
    21         len[np] = len[p] + 1, cnt[np] = 1, ft[np] = pos;
    22         memset( ch[np], 0, sizeof ch[np]);
    23         while( p && !ch[p][x]) ch[p][x] = np, p = fa[p];
    24         if( p == 0)
    25             fa[np] = 1;
    26         else
    27         {
    28             int q = ch[p][x];
    29             if( len[q] == len[p] + 1)
    30                 fa[np] = q;
    31             else
    32             {
    33                 int nq = ++tot;
    34                 memcpy( ch[nq], ch[q], sizeof ch[q]);
    35                 len[nq] = len[p] + 1, fa[nq] = fa[q], fa[q] = fa[np] = nq, ft[nq] = ft[q];
    36                 while( p && ch[p][x] == q)  ch[p][x] = nq, p = fa[p];
    37             }
    38         }
    39     }
    40 
    41     void toposort( void)
    42     {
    43         for(int i = 1; i <= len[last]; i++)   sum[i] = 0;
    44         for(int i = 1; i <= tot; i++)   sum[len[i]]++;
    45         for(int i = 1; i <= len[last]; i++)   sum[i] += sum[i-1];
    46         for(int i = 1; i <= tot; i++)   tp[sum[len[i]]--] = i;
    47         for(int i = tot; i; i--)   ft[fa[i]] = min(ft[fa[i]],ft[i]);
    48     }
    49 
    50     void go(char *ss)
    51     {
    52         toposort();
    53         for(int i=0,n=len[last],p,j;i<n;i++)
    54         {
    55             for(p=1,j=0;;j++)
    56             {
    57                 int c=ss[i+j]-'a';
    58                 if(!(i+j<n&&ch[p][c]&&ft[ch[p][c]]-j<i))
    59                     break;
    60                 p=ch[p][c];
    61             }
    62             j--;
    63             if(p==1)
    64                 printf("-1 %d
    ",(int)ss[i]);
    65             else
    66                 printf("%d %d
    ",j+1,ft[p]-j),i+=j;
    67         }
    68     }
    69 } sam;
    70 
    71 char ss[100005];
    72 
    73 int main(void)
    74 {
    75     //freopen("in.acm","r",stdin);
    76     int t,cs=1;cin>>t;
    77     while(t--)
    78     {
    79         sam.init();
    80         scanf("%s",ss);
    81         for(int i=0,len=strlen(ss);i<len;i++)   sam.add(ss[i]-'a',i);
    82         printf("Case #%d:
    ",cs++);
    83         sam.go(ss);
    84     }
    85     return 0;
    86 }
  • 相关阅读:
    【水】希望之花
    如何不用狄利克雷卷积证明莫比乌斯函数性质二
    【数学】gcd
    挂分宝典
    [luogu P6042]「ACOI2020」学园祭 题解
    [luogu P6041]「ACOI2020」布丁暗杀计划 题解
    11.19模拟
    「CSP-S2020」题解
    11.11模拟
    「洛谷P1445」樱花
  • 原文地址:https://www.cnblogs.com/weeping/p/7591747.html
Copyright © 2011-2022 走看看