HDU 5558 Alice's Classified Message(后缀自动机)

Alice’s Classified Message(传送门)

Time Limit : 16000/8000ms (Java/Other) Memory Limit : 131072/131072K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 2

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<i T+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 “[b]Case #X:[/b]” 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

2015ACM/ICPC亚洲区合肥站-重现赛(感谢中科大)

题意

给定一个字符串S,从下标 0 开始进行操作
每次对于下标 i , 输出下标 i 开始的子串中最长的在其他地方出现过的串的长度, 其它出现的位置要求起点在位置i之前, 然后 i 移动到这个长度之后继续操作.

如果存在则输出匹配的最长的子串的长度和它开始匹配的位置

如果不存在输出-1,和当前S[i]的字符的 ASCII 的值

解题思路

可以将题目问题转换为求解位置 i 开始的后缀和以位置[0...i1]开始的所有后缀中最大匹配的公共前缀长度,如此我们可以用后缀自动机,边匹配边构建后缀自动机

代码

/*除去冗长的头文件*/

const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 2e6 + 5;
const int INF = 0x3f3f3f3f;

/*len表示当前匹配的最长子串的长度,pos表示位置,hzp表示后缀转移*/
const int CHAR = 26;
struct SAM_Node {
    int len, pos;
    int id;
    SAM_Node *hzp, *next[CHAR];

    SAM_Node() {}
    SAM_Node(int _len) {
        len = _len;
        hzp = 0;
        mem(next, 0);
    }
    void Init(){
        mem(next, 0);
    }
} SAM_node[MAXN << 1], *SAM_root, *SAM_last;

int SAM_sz;

SAM_Node *newSAM_Node(int len) {
    SAM_node[SAM_sz].len = len;
    SAM_node[SAM_sz].id = SAM_sz;
    SAM_node[SAM_sz].Init();
    return &SAM_node[SAM_sz ++];
}

SAM_Node *newSAM_Node(SAM_Node *p) {
    SAM_node[SAM_sz] = *p;
    SAM_node[SAM_sz].id = SAM_sz;
    return &SAM_node[SAM_sz ++];
}

void SAM_Init() {
    SAM_sz = 0;
    SAM_root = SAM_last = newSAM_Node(0);
    SAM_node[0].pos = 0;
}

void SAM_add(int c, int pos) {
    SAM_Node *p = SAM_last, *np = newSAM_Node(p -> len + 1);
    np -> pos = pos;
    SAM_last = np;
    for(; p && !p -> next[c]; p = p -> hzp) {
        p -> next[c] = np;
    }
    if(!p) {
        np -> hzp = SAM_root;
    } else {
        SAM_Node *q = p -> next[c];
        if(p -> len + 1 == q -> len) {
            np -> hzp = q;
        } else {
            SAM_Node *nq = newSAM_Node(q);
            nq -> len = p -> len + 1;
            q -> hzp = nq;
            np -> hzp = nq;
            for(; p && p -> next[c] == q; p = p -> hzp) {
                p -> next[c] = nq;
            }
        }
    }
}


int T;
char S[MAXN];

int main() {
#ifndef ONLINE_JUDGE
    //FIN;
    //FOUT;
#endif
    IO_Init();
    scanf("%d", &T);
    int cas = 1;
    while(T --) {
        scanf("%s", S);
        SAM_Init();
        printf("Case #%d:\n", cas ++);
        int now = 0, n = strlen(S);
        while(now < n) {
            int length = 0;
            SAM_Node * cur = SAM_root;
            while(now < n && cur -> next[S[now] - 'a'] != 0) {
                cur = cur -> next[S[now] - 'a'];
                length ++;
                SAM_add(S[now] - 'a', now);
                now ++;
            }
            if(length == 0) {
                SAM_add(S[now] - 'a', now);
                printf("-1 %d\n", S[now ++]);
            } else {
                printf("%d %d\n", length, cur -> pos - length + 1);
            }
        }
    }


    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值